Beginner's Q: Why won't this search script work?
Posted: Thu Sep 11, 2003 2:44 pm
Most of this comes from a PHP book. I modified the code a bit to make it apply to my situation, and also to help me learn better. This is the search form:
Now here is the results page:
No matter what I enter in the search, the results page tells me I haven't entered anything. If I take out the bit of code that checks for and reports that error, I get no results at all. Also, if I take out all of the code and just have the results page echo $searchtype and $searchterm, I get nothing there either. It won't even echo back to me the search term I just typed. Anyone know what's going on here?
Code: Select all
<?
$title = "Vendor Search";
include ("testing.inc");
html_begin($title,$title);
?>
<form action="vresults.php" method="post">
Choose Search Type:<br>
<select name="searchtype">
<option value="Vname">Vendor Name
<option value="Vadd">Vendor Address
<option value="Vcity">Vendor City
</select>
<br>
Enter Search Term:<br>
<input name="searchterm" type=text>
<BR>
<input type=submit value="Search!">
</form>
<?
html_end();
?>Code: Select all
<?
$title = "Vendor Search Results";
include ("testing.inc");
html_begin($title,$title);
testing_connect ();
trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo "You have not entered search details. Do it over.";
exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
$query = "select * from vendors where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<p>Number of vendors found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<P><STRONG>".($i+1).". Vendor Name: ";
echo htmlspecialchars (stripslashes($row["Vname"]));
echo "</STRONG><br>Vendor Address: ";
echo htmlspecialchars (stripslashes($row["Vadd"]));
echo "<BR>Vendor City: ";
echo htmlspecialchars (stripslashes($row["Vcity"]));
echo "</p>";
}
html_end();
?>