PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Search Facility Posted by munky344 on 20 Oct 2008 at 11:23 AM
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.
Report
Re: Search Facility Posted by munky344 on 21 Oct 2008 at 9:42 AM
Hi there

I'm much closer with this too but not quite there. Any advice?

Cheers

<html>
<head></head>
<body>

<?php

// Connect to database
$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('patient',$conn) or trigger_error("SQL", E_USER_ERROR);

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!";
}

}
?>
: 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.
:

Report
Re: Search Facility Posted by tradm on 22 Oct 2008 at 2:29 AM
: Hi there
:
: I'm much closer with this too but not quite there. Any advice?
:
: Cheers
:
: <html>
: <head></head>
: <body>
:
: <?php
:
: // Connect to database
: $conn = mysql_connect('localhost','root') or trigger_error("SQL",
: E_USER_ERROR);
: $db = mysql_select_db('patient',$conn) or trigger_error("SQL",
: E_USER_ERROR);
:
: 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!";
: }
:
: }
: ?>
: : 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.
: :
:
:

Have a look at the attached script, remember to change the database connection parameters before running it.
Attachment: search.php (4914 Bytes | downloaded 104 times)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.