Jump to content

Displaying data retrieved from the database onto a form


BasiM

Recommended Posts

Good day
 
Hope someone can help me. I am working on an Intranet project, the aim is to create a medical intranet system.
 
Please help me with the following.
 
1. I have created users on my database(called medionline) and stored the relevant information in table employees.
2. I have a script which searches for the name and surname of the employees in the database
3. When a match is found, it then prints the details. The search functionality works but not as I want it to work, this is where I need help. It should do the following.
           a. Search for a user using either the name, surname or username
           b. When a match is found, it will then display all the details about the specific user(name, surname, id number, contact number, jobtile and username_ in a form, meaning a new form will have to be set up with these fields.
 
This will allow me to create buttons within the form with the users details where I can delete users, update users or create new users.
 
 
Your assistance in this will be highly appreciated.
 
 
This is my search.php page
 
 
 
<?php
/*
 * author :basetsanamnl0@gmail.com
 *
 *
 *
 */
include 'connection.php';
    $connection = new createConnection(); // creating connection
    $connect = $connection->connectToDatabase(); // connect to database
    $connection->selectDatabase(); // select database
 
 //-------------------QUERY--------------------------------------//
 
 $query = $_GET['query'];
    // gets value sent over search form
    
    $min_length = 3;
    // you can set minimum length of the query if you want
    
    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
        
        $query = htmlspecialchars($query);
        // changes characters used in html to their equivalents, for example: < to >
        
        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection
        
        $raw_results = mysql_query("SELECT * FROM employees
            WHERE (`name` LIKE '%".$query."%') AND (`surname` LIKE '%".$query."%')") or die(mysql_error());
            
        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table
        
        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
        
        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
            
            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
            
                echo "<p><h3>".$results['name']."</h3>".$results['surname']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }
            
        }
        else{ // if there is no matching rows do following
            echo "No results";
        }
        
    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
 
 
 
This is my search.index.php page
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Medi_online</title>
<link type="stylesheet" rel="stylesheet" href="main.css"/>
<script src="buttons.js"></script>
<script src="connect.js"></script>
<script src="slider.js"></script>
</head>
<!---Website Design by Basetsana Manele
  medi_online intranet system 2013
        Contact: basetsanamnl0@gmail.com  -->
<body>
<!--Twitter and Facebook connect tabs---->
<div class="s-connect">
  <div class="Fbutton">
    <div class="facebook"> <img src="images/social_media/Facebook.png"/> </div>
    <div class="cont"></div>a
  </div>
  <div id="clear"></div>
  <div class="Tbutton">
    <div class="twitter"> <img src="images/social_media/twitter.png"/> </div>
    <div class="Tcont"></div>
  </div>
  <div id="clear"></div>
</div>
<div id="clear"></div>
<!-------------------------------------------------->
<div id="weblinks">
  <div id="link"><a href="../medionline/logout.php" target="_new">Logout | </a></div>
  <div id="link"><a href="../medionline/admin_login.php"target=_"new">Admin Login </a></div>
</div>
<!-------Banner------------------>
<div id="banner">
</div>
 
<!-------Main Navigation--------->
<div id="nav">
  <div class="nav_bs" style="width:175px;" ><a style="color:#e1e1e1;" href="index.html">INTRANET HOME</a></div>
  <div class="nav_bs" style="width:180px;" ><a style="color:#e1e1e1;" href="about_fisha.html">EMPLOYEE TOOLS</a></div>
  <div class="nav_bs" style="width:250px;" ><a style="color:#e1e1e1;" href="healthcare.html">COLLOBORATION TOOLS</a></div>
  <div class="nav_bs" style="width:190px;" ><a style="color:#e1e1e1;" href="training.html">DEPARTMENT HUB</a></div></div>
<!-------Paitent information sign up------->
<div id="container_main">
  <h3 align="center">SEARCH USER</h3>
   <br/>
<h5 align="justify"> Please enter either name, surname or username to search.</h5>
   <br/>
  <form action="search.php" method="GET">
  <table width="500" border="0" cellpadding="0">
  <input type="text" name="query" />
  <input type="submit" value="Search" />
 
  <tr>
    <td> </td>
  </tr>
  </form>
</div>

</body>
</html>

Link to comment
Share on other sites

Do you already have a form for creating new users? You should use the same form, but modify it to populate the input fields with the data you retrieved from the database.

 

Here is one possible workflow. Upon retrieving the user information from the database, put the array of data into an array (say $user, for example) and then call the form. The form could have logic such as this

 

 

//define default values to populate form
$fname = '';
$lname = '';
$phone = '';
 
//If user defined replace default values with user values
if(isset($user))
{
    $fname = $user['fname'];
    $lname = $user['lname'];
    $phone = $user['phone'];
}
 
//Then use the variables for populating the form input fields
echo "First Name: <input type=\"text\" name=\"fname\" value=\"{$fname}\" ><br>\n";
echo "Last Name: <input type=\"text\" name=\"fname\" value=\"{$lname}\" ><br>\n";
echo "Phone: <input type=\"text\" name=\"fname\" value=\"{$phone}\" ><br>\n";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.