Return query to my webpage

Good morning,

I am very new to php and this is my first post here. So here goes. I have an html page with a form where the user can type a first name and a last name. When submitted the php page will connect to and query my database returning all records that contain the strings from the form. Once the query executes I wrote a simple loop to echo the results… All of this to this point is working.

What I need to do is show the results formatted on an html page with buttons on each record that will allow the user to select the correct result if there is more than one.

[php]

<?php //Start session session_start(); //Include database connection details require_once('config.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $fname = clean($_POST['fname']); $lname = clean($_POST['lname']); //Query the Database $query = "select * from Patient where FName like '%$fname%' and LName like '%$lname%' "; $result = mysql_query($query) or die(mysql_error()); //Display Results while ($row = mysql_fetch_assoc($result)){ echo 'ID: '.$row['ID']; echo '
First Name: '.$row['FName']; echo '
Last Name: '.$row['LName']; echo '

'; } ?>

[/php]

Hi deslyxia,

what you need to do is very simple, just instead of printing the
you have to write the HTML tags for new rows as following:

//Query the Database
$query = "select * from Patient where FName like '%$fname%' and LName like '%$lname%' ";
$result = mysql_query($query) or die(mysql_error());

//Display Results
    
    // create table:

echo "<table alingn=\"center\" width=\"400px;\">";
while ($row = mysql_fetch_assoc($result)){
	  echo "<tr>
				<td>ID</td><td>".$row['ID']."</td>
				<td>FirstName</td><td>".$row['FName']."</td>
				<td>LastName</td><td>".$row['LName']."</td>
			</tr>";
}

echo "</table>";	

hope it helps
:slight_smile:

That makes total sense as far as formatting in a table goes.

The issue is that the php code you see is in a separate file from my html page. Once i do the query … get the results how do i get all of that data back to the original html page and display it there in a nice neat table.

I made some edits to the suggested php code just to get the table to format correctly. It is working well at this point but it still shows this table on a blank white page because the php document that its in is external to my html document.

basically i need a way of getting the query results back to the main html page and THEN do this little php clip that will show it in a table.

[php]
//Display Results
echo “<table alingn=“center” width=“400px;”>”;
echo "


ID
FirstName
LastName
";
while ($row = mysql_fetch_assoc($result)){
echo "
“.$row[‘ID’].”
“.$row[‘FName’].”
“.$row[‘LName’].”
";
}
echo "</table>";   

[/php]

The more i read into this the more i see that I am going to need to learn how to use AJAX to do this. I realize that this post is not in the AJAX section of the forum but in the hopes of not double posting hopefully someone can show me here.

I have a form, when the user submits it runs a php script (in a separate file), I need to somehow get the results of the php query contained in that file into a table on the original page. (i am assuming this is where AJAX comes in)

Below is the form and the php script…

<form id="searchForm" name="searchForm" method="post" action="psearch.php" autocomplete="off">
		  		<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
   		  		    <tr>
   		    	  	  <td width="112"><b>First Name</b></td>
    			      <td width="188"><input name="fname" type="text" class="textfield" id="fname" /></td>
   					</tr>
		    		<tr>
   					  <td><b>Last Name</b></td>
      				  <td><input name="lname" type="text" class="textfield" id="lname" /></td>
    				</tr>
    				<tr>
      		   		  <td>&nbsp;</td>
      		   	      <td><input type="submit" name="Submit" value="Search" /></td>
    				</tr>
	      		</table>
			</form>

[php]

<?php //Start session session_start(); //Include database connection details require_once('config.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $fname = clean($_POST['fname']); $lname = clean($_POST['lname']); //Query the Database $query = "select * from Patient where FName like '%$fname%' and LName like '%$lname%' "; $result = mysql_query($query) or die(mysql_error()); $> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service