swollenpickles Posted October 26, 2007 Share Posted October 26, 2007 Firstly, I need to point out, I'm a total PHP noob! 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."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/ Share on other sites More sharing options...
GingerRobot Posted October 26, 2007 Share Posted October 26, 2007 So what happens? Do you get an error? I say this so many times - we need to know what the problem is before we do anything! Help us to help you. Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-378512 Share on other sites More sharing options...
swollenpickles Posted October 26, 2007 Author Share Posted October 26, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-378514 Share on other sites More sharing options...
GingerRobot Posted October 26, 2007 Share Posted October 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-378516 Share on other sites More sharing options...
swollenpickles Posted October 26, 2007 Author Share Posted October 26, 2007 Thanks. I think it's almost right, except now it prints out this message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\ASSIGNMENT3\search.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-378568 Share on other sites More sharing options...
swollenpickles Posted October 27, 2007 Author Share Posted October 27, 2007 *bump* cos I still can't figure out why I'm getting this: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\ASSIGNMENT3\search.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-379087 Share on other sites More sharing options...
teng84 Posted October 27, 2007 Share Posted October 27, 2007 maybe your query is invalid show your query Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-379088 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 try mysql_fetch_assoc or mysql_num_rows but i have a feelilng your query is the prob Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-379089 Share on other sites More sharing options...
kratsg Posted October 27, 2007 Share Posted October 27, 2007 <?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."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74864-simple-php-search-not-searching/#findComment-379114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.