munky334 Posted October 20, 2008 Share Posted October 20, 2008 Hi there, I need some help please. I'm a newbie so please be patient!. I have created the following database: CREATE TABLE patientdemo (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30), identitynumber VARCHAR(30)); INSERT INTO patientdemo (firstname, lastname, identitynumber) VALUES ( "Alexa", "McDonald", "1234567890987"), ( "Devie", "Slater", "987654321124" ) I then created an HTML form which has two section. It has a section where users can enter information they want to search for and then a table where I would like information to display in once the search has been preformed. <html> <form method="POST" action="search.php"> Search for patient:<br /> <p> First Name: <input type="text" id="firstname" name="firstname" /><br /> Last Name: <input type="text" id="lastname" name="lastname" /><br /> Identity Number: <input type="text" id="identitynumber" name="identitynumber" /><br /> <p> <input type="submit" value="Search!" /> </form> <html> <head> <table border cellspacing=0 cellpadding=5> <caption align=bottom> </caption> <tr> <td colspan=10 rowspan=10></td> <th colspan=10 align=center>Patient Report</th> </tr> <tr> <th>First Name</th> <th>Last Name</th> <th>Identity Number</th> </tr> I then wrote a search.php form that handles the code to display the search result. <?php // Connect to DB mysql_connect("localhost","root"); mysql_select_db("patient"); $firstname = mysql_real_escape_string($_POST['firstname']); $lastname = mysql_real_escape_string ($_POST['lastname']); $identitynumber = mysql_real_escape_string ($_POST['identitynumber']); // Perform the fulltext search $query = "SELECT id, firstname, lastname, identitynumber FROM patientdemo"; $result = mysql_query($query); // If results were found, output them if (mysql_num_rows($result) > 0) { printf //Display in the following HTML Table ("</form> <html> <head> <table border cellspacing=0 cellpadding=5> <caption align=bottom> </caption> <tr> <td colspan=10 rowspan=10></td> <th colspan=10 align=center>Patient Report</th> </tr> <tr> <th>First Name</th> <th>Last Name</th> <th>Identity Number</th> </tr>"); } else { printf("No results found"); } ?> So anyway , the problem is that I want to be able to search for information using the input boxes on the HTML form , and then the info must display in the table (also on the HTML form). How would I go about this. Once again I'm a newbie so patients please. Thanking you in advance. Link to comment https://forums.phpfreaks.com/topic/129269-search-facility/ Share on other sites More sharing options...
GKWelding Posted October 20, 2008 Share Posted October 20, 2008 <html> <head></head> <body> <?php if(isset($_POST['submit'])){ $firstname = mysql_real_escape_string($_POST['firstname']); $lastname = mysql_real_escape_string ($_POST['lastname']); $identitynumber = mysql_real_escape_string ($_POST['identitynumber']); if(isset($firstname)){ $where .= "firstname = "; $where .= $firstname; } if(isset($lastname)){ if(isset($firstname)){ $where .= " AND "; } $where .= "lastname = "; $where .= $lastname; } if(isset($identitynumber)){ if(isset($firstname) OR isset($lastname)){ $where .= " AND "; } $where .= "identitynumber = "; $where .= $identitynumber; } $sql = mysql_query("SELECT id, firstname, lastname, identitynumber FROM patientdemo WHERE $where"); if(mysql_num_rows($sql)>0){ WHILE($row = mysql_fetch_array($sql)){ $display .= "<tr><th>"; $display .= $row['firstname']; $display .= "</th><th>"; $display .= $row['lastname']; $display .= "</th><th>"; $display .= $row['identitynumber']; $display .= "</th></tr>"; } }else{ $display = "No Results Found!"; } } ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Search for patient:<br /> <p> First Name: <input type="text" id="firstname" name="firstname" /><br /> Last Name: <input type="text" id="lastname" name="lastname" /><br /> Identity Number: <input type="text" id="identitynumber" name="identitynumber" /><br /> <p> <input type="submit" value="Search!" /> </form> <table border cellspacing=0 cellpadding=5> <caption align=bottom> </caption> <tr> <td colspan=10 rowspan=10></td> <th colspan=10 align=center>Patient Report</th> </tr> <?php if(isset($display)){ echo $display; } ?> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/129269-search-facility/#findComment-670341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.