Beginner's Q: Why won't this search script work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
baseband
Forum Newbie
Posts: 2
Joined: Thu Sep 11, 2003 2:44 pm

Beginner's Q: Why won't this search script work?

Post by baseband »

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:

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();
?>
Now here is the results page:

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();
?>
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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Click the last link in my signature, and read about the register_globals issue in newer versions of PHP.
You wrote this using a book. most likely, the books author wrote it while PHP was still not using register_globals = Off.

(You'll understand.)
baseband
Forum Newbie
Posts: 2
Joined: Thu Sep 11, 2003 2:44 pm

Post by baseband »

D'oh! I actually did read that thread, but it was about three months ago when I registered, and I'd forgotten about it. The script works now. Thanks for giving me a friendly reminder instead of the flame I probably deserved. :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

baseband wrote:Thanks for giving me a friendly reminder instead of the flame I probably deserved. :)
Well, it's not to late... :twisted:
It's not uncommon that users miss the "STICKY!" part in the forums, so I'm not surprised or bothered anymore.

Happy coding.
Post Reply