Jump to content

simple PHP search not searching


swollenpickles

Recommended Posts

Firstly, I need to point out, I'm a total PHP noob!  :P

 

I'm trying to put together a basic php search page, that just searches one table and then prints out the results. I had a look for tutorials here but couldn't find anything similar (unless I missed it).

 

I've put together the following code for the search script, and looked over it over and over again but can't seem to find out where it's wrong, or why it's not working.

 

I'm really hoping someone can help with this cos it's driving me mad!!

 

thanks

 

p.s

here's the code (I could post the form code if needed):

 

<?php
	// This is only displayed if they have submitted the form
	if ($searching =="yes") {
		print "<p><h2>Search Results</h2></p>";
		//If they did not enter a search term we give them an error
		if ($find == "") {
		print "<p>You forgot to enter a search term. Can't search without a 
		search term!</p>";
		exit;
		}

		// If they enter a search term connect to the database.
		require_once("inc/connection.php");

		// filter out white space, tags etc.
		$find = strtoupper($find);
		$find = strip_tags($find);
		$find = trim ($find);

		// Search the database for the search term entered by the user.
		$searchresults = mysql_query("SELECT * FROM comic WHERE upper($field) LIKE '%$find%'");
		print $searchresults;
		// display the results
		while($result = mysql_fetch_array( $searchresults )) {
			print $result['ComicTitle'];
			print "<br />";
			print $result['ReleaseDate'];
			print "<br />";
			print $result['ComicPrice'];
			print "<br />";
			print $result['ComicDescription'];
			print "<br />";
		}

		//This counts the number or results. If no result, they get the no matching entries message.
		$anymatches=mysql_num_rows($searchresults);
		if ($anymatches == 0) {
			print "You searched for " . $find . ". Sorry, no entries matched your 
			search.";
		}
	}
	?>

Link to comment
Share on other sites

Thanks. I don't get any errors. The search page just loads up again, looking the way it did before hitting submit.

 

Here's the form code if it helps?

 

		<form name="search" method="post" action="<?=$PHP_SELF?>">
		<p>Enter the comic you'd like to search for:</p>
		<p><input type="text" name="find" value="<?php print $find; ?>" /></p>
		<!-- <input type="hidden" name="searching" value="yes" /> -->
		<input type="submit" name="search" value="Search" />
	</form>

Link to comment
Share on other sites

Still a little unclear. The only thing i can assume is that the code you posted is all of your php code - in which case, im guessing that the problem is you've not retrieved the relevant variables from the $_POST array, and you server is set up with register_globals off (a good thing!)

 

So, try this code:

 

<?php
	// This is only displayed if they have submitted the form
	if (isset($_POST['search'])) {//this is a method people would use for checking to see if the form has been submitted
	//the name we use is the submit button's name - in this case, 'search'
		$find = $_POST['find'];//set $find to its value in the post array
		print "<p><h2>Search Results</h2></p>";
		//If they did not enter a search term we give them an error
		if ($find == "") {
		print "<p>You forgot to enter a search term. Can't search without a 
		search term!</p>";
		exit;
		}

		// If they enter a search term connect to the database.
		require_once("inc/connection.php");

		// filter out white space, tags etc.
		$find = strtoupper($find);
		$find = strip_tags($find);
		$find = trim ($find);

		// Search the database for the search term entered by the user.
		$searchresults = mysql_query("SELECT * FROM comic WHERE upper($field) LIKE '%$find%'");
		print $searchresults;
		// display the results
		while($result = mysql_fetch_array( $searchresults )) {
			print $result['ComicTitle'];
			print "<br />";
			print $result['ReleaseDate'];
			print "<br />";
			print $result['ComicPrice'];
			print "<br />";
			print $result['ComicDescription'];
			print "<br />";
		}

		//This counts the number or results. If no result, they get the no matching entries message.
		$anymatches=mysql_num_rows($searchresults);
		if ($anymatches == 0) {
			print "You searched for " . $find . ". Sorry, no entries matched your 
			search.";
		}
	}
	?>

 

Notice the change to how you check if the form has been submitted - you don't need your hidden field.

Link to comment
Share on other sites

<?php
	// This is only displayed if they have submitted the form
	if ($searching =="yes") {
		print "<p><h2>Search Results</h2></p>";
		//If they did not enter a search term we give them an error
		if ($find == "") {
		print "<p>You forgot to enter a search term. Can't search without a 
		search term!</p>";
		exit;
		}

		// If they enter a search term connect to the database.
		require_once("inc/connection.php");

		// filter out white space, tags etc.
		$find = mysql_real_escape_string(trim(strip_tags(strtoupper($find))));

		// Search the database for the search term entered by the user.
		$q = "SELECT * FROM comic WHERE upper($field) LIKE '%$find%'";
		$searchresults = mysql_query($q);
		// display the results
		while($result = mysql_fetch_array( $searchresults )) {
			print $result['ComicTitle'];
			print "<br />";
			print $result['ReleaseDate'];
			print "<br />";
			print $result['ComicPrice'];
			print "<br />";
			print $result['ComicDescription'];
			print "<br />";
		}

		//This counts the number or results. If no result, they get the no matching entries message.
		$anymatches=mysql_num_rows($searchresults);
		if ($anymatches == 0) {
			print "You searched for " . $find . ". Sorry, no entries matched your 
			search.";
		}
	}
	?>

 

I added and changed the following:

//added
mysql_real_escape_string($find);
//changed
// filter out white space, tags etc.
$find = mysql_real_escape_string(trim(strip_tags(strtoupper($find))));

 

Changed from this:

		// Search the database for the search term entered by the user.
		$searchresults = mysql_query("SELECT * FROM comic WHERE upper($field) LIKE '%$find%'");
		print $searchresults;
		// display the results

 

To this:

		// Search the database for the search term entered by the user.
		$q = "SELECT 'ComicTitle','ReleaseDate','ComicPrice','ComicDescription' FROM comic WHERE $field LIKE '%$find%'";
		$searchresults = mysql_query($q) or die("I'm sorry, the query encountered an error.".mysql_error());
		// display the results

Changed from (*) to ('ComicTitle','ReleaseDate','ComicPrice','ComicDescription'), removed the upper($field) part and changed to $field [please check why you have upper(), since it's not a php function]

 

 

If you want error checking to see how the query in question is set-up, add an echo $q;

 

		// Search the database for the search term entered by the user.
		$q = "SELECT 'ComicTitle','ReleaseDate','ComicPrice','ComicDescription' FROM comic WHERE $field LIKE '%$find%'";
		$searchresults = mysql_query($q) or die("I'm sorry, the query encountered an error.".mysql_error());
		echo $q;//output query string to make sure it is set-up correctly
		// display the results

 

Your new code:

 

<?php
	// This is only displayed if they have submitted the form
	if ($searching =="yes") {
		print "<p><h2>Search Results</h2></p>";
		//If they did not enter a search term we give them an error
		if ($find == "") {
		print "<p>You forgot to enter a search term. Can't search without a 
		search term!</p>";
		exit;
		}

		// If they enter a search term connect to the database.
		require_once("inc/connection.php");

		// filter out white space, tags etc.
		$find = mysql_real_escape_string(trim(strip_tags(strtoupper($find))));

		// Search the database for the search term entered by the user.
		$q = "SELECT 'ComicTitle','ReleaseDate','ComicPrice','ComicDescription' FROM comic WHERE $field LIKE '%$find%'";
		$searchresults = mysql_query($q) or die("I'm sorry, the query encountered an error.".mysql_error());
		// display the results
		while($result = mysql_fetch_array( $searchresults )) {
			print $result['ComicTitle'];
			print "<br />";
			print $result['ReleaseDate'];
			print "<br />";
			print $result['ComicPrice'];
			print "<br />";
			print $result['ComicDescription'];
			print "<br />";
		}

		//This counts the number or results. If no result, they get the no matching entries message.
		$anymatches=mysql_num_rows($searchresults);
		if ($anymatches == 0) {
			print "You searched for " . $find . ". Sorry, no entries matched your 
			search.";
		}
	}
	?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.