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

Okay, done.
Now I get this error:
SELECT * FROM users WHERE info LIKE '%HELLO%'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIKE '%HELLO%'' at line 1

With 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.  
  43. //Now we search for our search term, in the field the user specified
  44.  
  45. $sql = "SELECT * FROM users WHERE info LIKE '%$find%'";
  46.  
  47. echo $sql;
  48.  
  49. $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE '%$find%'") or die(mysql_error());
  50.  
  51. //And we display the results
  52. while($result = mysql_fetch_array( $data ))
  53. {
  54. echo $result['fname'];
  55. echo " ";
  56. echo $result['lname'];
  57. echo "<br>";
  58. echo $result['info'];
  59. echo "<br>";
  60. echo "<br>";
  61. }
  62.  
  63. //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  64. $anymatches=mysql_num_rows($data);
  65. if ($anymatches == 0)
  66. {
  67. echo "Sorry, but we can not find an entry to match your query<br><br>";
  68. }
  69.  
  70. //And we remind them what they searched for
  71. echo "<b>Searched For:</b> " .$find;
  72. }
  73. ?>
  74. </body>
  75. </html>

this is the code in the mysql database
mysql Syntax (Toggle Plain Text)
  1. CREATE TABLE users (fname VARCHAR(30), lname VARCHAR(30), info BLOB);
  2.  
  3. INSERT INTO users VALUES ( "Jim", "Jones", "In his spare time Jim enjoys biking, eating pizza, and classical music" ), ( "Peggy", "Smith", "Peggy is a water sports enthusiast who also enjoys making soap and selling cheese" ),( "Maggie", "Martin", "Maggie loves to cook itallian food including spagetti and pizza" ),( "Tex", "Moncom", "Tex is the owner and operator of The Pizza Palace, a local hang out joint" )
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote