Php mysql search form

Ok Im new at php mysql programimg from scratch. Been reading some tutorials. I have only found one issue on several tutorials. I setup a database. It has fields and values. When I search the database with

$query = mysql_query(“SELECT * FROM details WHERE name=‘john doe’”);

The results.php script works nd returns the data. When I use a search form to try to populate this

$query = mysql_query(“SELECT * FROM details WHERE name=’$search’”);

It comes back no records. I had another form it gave me the same issue

Here are the scripts.

search.htm

<? $username="xxxxxxx"; $password="xxxxxx"; $database="devgrou1_gtaddress"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM contacts WHERE first='$searchfirst'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "Database Output

"; ?> <? $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); ?> <? ++$i; } echo "
Name Phone Mobile Fax E-mail Website
<? echo "$first $last"; ?> <? echo "$phone"; ?> <? echo "$mobile"; ?> <? echo "$fax"; ?> ">E-mail ">Website
";

?>

 

 

Results.php

"); print (""); print (""); print (""); print (""); print (""); } //below this is the function for no record!! if (!$variable1) { print ("$XX"); } //end ?>
id name telephone birthday
<? $hostname = "localhost"; // Our DB server. $username="xxxxxxx"; $password="xxxxxx"; $usertable = "details"; // The name of the table you made. $dbName = "devgrou1_gtaddress"; // This is the name of the database you made.

MYSQL_CONNECT($hostname, $username, $password) OR DIE(“DB connection unavailable”);
@mysql_select_db( “$dbName”) or die( “Unable to select database”);
?>

<? //error message (not found message)begins $XX = "No Record Found, to search again please close this window"; //query details table begins $query = mysql_query("SELECT * FROM details WHERE name='$search'"); while ($row = @mysql_fetch_array($query)) { $variable1=$row["id"]; $variable2=$row["name"]; $variable3=$row["telephone"]; $variable4=$row["birthday"]; //table layout for results print ("
$variable1$variable2$variable3$variable4

Please help. here is the url of the test site name john doe should work

http://dev3.curtiswallis.com/test2/search2.htm
http://dev3.curtiswallis.com/test2/results2.php

Curt

From your code it is not clear where do you define these php variables: $searchfirst, and $search?
If they come from html form - where is this form?

If ‘search’ is the name of text field in your html form, and request method is POST, try to use this in your query:

[php]
$query = mysql_query(“SELECT * FROM details WHERE name=’”.mysql_real_escape_string($_POST[“search”])."’");
[/php]

Ok I pasted the wrong script. But I did update the above post but still not working.

Search2.htm

Untitled Document


Search database:

results2.php

"); print (""); print (""); print (""); print (""); print (""); } //below this is the function for no record!! if (!$variable1) { print ("$XX"); } //end ?>
id name telephone birthday
<? $hostname = "localhost"; // Our DB server. $username="xxxxx"; $password="xxxxx"; $usertable = "details"; // The name of the table you made. $dbName = "devgrou1_gtaddress"; // This is the name of the database you made.

MYSQL_CONNECT($hostname, $username, $password) OR DIE(“DB connection unavailable”);
@mysql_select_db( “$dbName”) or die( “Unable to select database”);
?>

<? //error message (not found message)begins $XX = "No Record Found, to search again please close this window"; //query details table begins $query = mysql_query("SELECT * FROM details WHERE name='".mysql_real_escape_string($_POST["search"])."'"); { $variable1=$row["id"]; $variable2=$row["name"]; $variable3=$row["telephone"]; $variable4=$row["birthday"]; //table layout for results print ("
$variable1$variable2$variable3$variable4

It looks like in your code for results2.php you’re missing this important line:

[php]
while ($row = @mysql_fetch_array($query))
[/php]

It need to be added right after the row where you call mysql_query() function.

Sponsor our Newsletter | Privacy Policy | Terms of Service