View Single Post
Old Mar 26th, 2008, 1:39 PM   #4
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 3 Arnack is on a distinguished road
Re: Simple PHP Search Script

Okay, thanks... and I tried that. Now it's bringing the error that I forgot to type something in the search for some reason.. (when I am typing in the search is still does the same thing..).

EDIT: nevermind, I saw that 'find' variable was not defined either. Now I have this code:

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>arnack.com Gaming Wesbite Database</title>
  4. </head>
  5. <body>
  6. <h2>Search</h2>
  7. <form name="search" method="post" action="<?=$PHP_SELF?>">
  8. Seach for: <input type="text" name="find" /> in
  9. <Select NAME="field">
  10. <Option VALUE="fname">First Person Shooters</option>
  11. <Option VALUE="lname">Role PLaying Games</option>
  12.  
  13. <Option VALUE="info">Real Time Strategy</option>
  14. </Select>
  15. <input type="hidden" name="searching" value="yes" />
  16. <input type="submit" name="search" value="Search" />
  17. </form>
  18. <?
  19. $searching = $_POST['searching'];
  20. $find = $_POST['find'];
  21. //This is only displayed if they have submitted the form
  22. if ($searching =="yes")
  23. {
  24. echo "<h2>Results</h2><p>";
  25.  
  26. //If they did not enter a search term we give them an error
  27. if ($find == "")
  28. {
  29. echo "<p>You forgot to enter a search term";
  30. exit;
  31. }
  32.  
  33. // Otherwise we connect to our Database
  34. mysql_connect("localhost", "arnackco_Arnack", "oompa123") or die(mysql_error());
  35. mysql_select_db("arnackco_search") or die(mysql_error());
  36.  
  37. // We preform a bit of filtering
  38. $find = strtoupper($find);
  39. $find = strip_tags($find);
  40. $find = trim ($find);
  41.  
  42. //Now we search for our search term, in the field the user specified
  43. $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
  44.  
  45. //And we display the results
  46. while($result = mysql_fetch_array( $data ))
  47. {
  48. echo $result['fname'];
  49. echo " ";
  50. echo $result['lname'];
  51. echo "<br>";
  52. echo $result['info'];
  53. echo "<br>";
  54. echo "<br>";
  55. }
  56.  
  57. //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  58. $anymatches=mysql_num_rows($data);
  59. if ($anymatches == 0)
  60. {
  61. echo "Sorry, but we can not find an entry to match your query<br><br>";
  62. }
  63.  
  64. //And we remind them what they searched for
  65. echo "<b>Searched For:</b> " .$find;
  66. }
  67. ?>
  68. </body>
  69. </html>
Now it says:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/arnackco/public_html/index.php on line 46

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/arnackco/public_html/index.php on line 58
Sorry, but we can not find an entry to match your query
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote