Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Simple PHP Search Script (http://www.programmingforums.org/showthread.php?t=15487)

Arnack Mar 26th, 2008 1:18 PM

Simple PHP Search Script
 
I put this all on index.php:
:

  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 Name</option>
  11. <Option VALUE="lname">Last Name</option>
  12. <Option VALUE="info">Profile</option>
  13. </Select>
  14. <input type="hidden" name="searching" value="yes" />
  15. <input type="submit" name="search" value="Search" />
  16. </form>
  17. <?
  18. error_reporting(E_ALL);
  19. //This is only displayed if they have submitted the form
  20. if ($searching =="yes")
  21. {
  22. echo "<h2>Results</h2><p>";
  23.  
  24. //If they did not enter a search term we give them an error
  25. if ($find == "")
  26. {
  27. echo "<p>You forgot to enter a search term";
  28. exit;
  29. }
  30. // Otherwise we connect to our Database
  31. mysql_connect("localhost", "arnackco_Arnack", "oompa123") or die(mysql_error());
  32. mysql_select_db("arnackco_search") or die(mysql_error());
  33.  
  34. // We preform a bit of filtering
  35. $find = strtoupper($find);
  36. $find = strip_tags($find);
  37. $find = trim ($find);
  38.  
  39. //Now we search for our search term, in the field the user specified
  40. $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
  41.  
  42. //And we display the results
  43. while($result = mysql_fetch_array( $data ))
  44. {
  45. echo $result['fname'];
  46. echo " ";
  47. echo $result['lname'];
  48. echo "<br>";
  49. echo $result['info'];
  50. echo "<br>";
  51. echo "<br>";
  52. }
  53.  
  54. //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  55. $anymatches=mysql_num_rows($data);
  56. if ($anymatches == 0)
  57. {
  58. echo "Sorry, but we can not find an entry to match your query<br><br>";
  59. }
  60.  
  61. //And we remind them what they searched for
  62. echo "<b>Searched For:</b> " .$find;
  63. }
  64. ?>
  65. </body>
  66. </html>

Problem is outputed as:
Notice: Undefined variable: searching in /home/arnackco/public_html/index.php on line 20

Any help please? :(

big_k105 Mar 26th, 2008 1:26 PM

Re: Simple PHP Search Script
 
The problem is your not pulling the searching variable out of the POST data.
What you need to do first is this:

:

  1. $searching = $_POST['searching'];


Put that above the if statement and it will work.

Freaky Chris Mar 26th, 2008 1:31 PM

Re: Simple PHP Search Script
 
Im no PHP expert but, im guessing that when the user submits the form it reloads to its own page then checks what the user entered based on the code provided.

You are trying to acces a variable $searching, which doesn't exist this is because you want the result from the previous page when it was submitted no? So since you are using the post method for the form you need to use this

:

  1. $searching=$_POST['searching'];


or just directly link it in instead of assigning it to the variable searching.

Chris

o Big K beat me, o well lol

Arnack Mar 26th, 2008 1:39 PM

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:

:

  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

big_k105 Mar 26th, 2008 1:58 PM

Re: Simple PHP Search Script
 
:

  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.                         <option VALUE="info">Real Time Strategy</option>
  13.                 </select>
  14.                 <input type="hidden" name="searching" value="yes" />
  15.                 <input type="submit" name="search" value="Search" />
  16.         </form>
  17.         <?
  18.         $searching = $_POST['searching'];
  19.         $find = $_POST['find'];
  20.         //This is only displayed if they have submitted the form
  21.         if ($searching =="yes") {
  22.                 echo "<h2>Results</h2><p>";
  23.  
  24.                 //If they did not enter a search term we give them an error
  25.                 if ($find == "") {
  26.                         echo "<p>You forgot to enter a search term";
  27.                         exit;
  28.                 }
  29.  
  30.                 // Otherwise we connect to our Database
  31.                 mysql_connect("localhost", "arnackco_Arnack", "oompa123") or die(mysql_error());
  32.                 mysql_select_db("arnackco_search") or die(mysql_error());
  33.  
  34.                 // We preform a bit of filtering
  35.                 $find = strtoupper($find);
  36.                 $find = strip_tags($find);
  37.                 $find = trim ($find);
  38.  
  39.                 //Now we search for our search term, in the field the user specified
  40.                 $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
  41.                 if ($data) {
  42.                         //And we display the results
  43.                         while($result = mysql_fetch_array( $data )) {
  44.                                 echo $result['fname'];
  45.                                 echo " ";
  46.                                 echo $result['lname'];
  47.                                 echo "<br>";
  48.                                 echo $result['info'];
  49.                                 echo "<br>";
  50.                                 echo "<br>";
  51.                         }
  52.  
  53.                         //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  54.                         $anymatches=mysql_num_rows($data);
  55.                         if ($anymatches == 0) {
  56.                                 echo "Sorry, but we can not find an entry to match your query<br><br>";
  57.                         }
  58.  
  59.                         //And we remind them what they searched for
  60.                         echo "<b>Searched For:</b> " .$find;
  61.                 } else {
  62.                         echo "Error while sending query";
  63.                 }
  64.         }
  65.         ?>
  66. </body>
  67. </html>


Give this a try, I didn't test it though. All I did was add an if statement to check the $data variable before going through the results. I think you could also try changing this line $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); to $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'") or die("Query Error");

Arnack Mar 26th, 2008 2:20 PM

Re: Simple PHP Search Script
 
Alright it does print out Query Error.. :(

big_k105 Mar 26th, 2008 2:39 PM

Re: Simple PHP Search Script
 
Ok, so atleast now you know there is a problem when sending your query. might want to check your SQL statement.

Change this:
:

  1. #
  2. //Now we search for our search term, in the field the user specified
  3. #
  4. $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");

to:
:

  1. //Now we search for our search term, in the field the user specified
  2. $sql = "SELECT * FROM users WHERE upper($field) LIKE'%$find%'";
  3. echo $sql;
  4. $data = mysql_query($sql) or die(mysql_error());


This way you can see exactly what is being sent to the database.

Sane Mar 26th, 2008 2:41 PM

Re: Simple PHP Search Script
 
Try printing out the variables and the query before sending them off. Verify that the query and related variables are sound and correct.

Also, I'm not sure but it could matter that you had an apostraphe directly after the word 'LIKE'.

Then finally, add a mysql_error() command to see what happened.

:

  1. $qry = "SELECT * FROM users WHERE upper($field) LIKE '%$find%'";
  2. echo "Query: $qry<br />Field: $field<br />Find: $find<br />";
  3. $data = mysql_query($qry);
  4.  
  5. echo mysql_error();


Arnack Mar 26th, 2008 2:45 PM

Re: Simple PHP Search Script
 
Okay. Here is my MySQL code:

:

  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" )

I don't see anything wrong here.. hmm..
:

  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 upper($field) LIKE'%$find%'";
  46.  
  47.       echo $sql;
  48.  
  49.       $data = mysql_query($sql) 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>


error: (i typed in testsearch in the search)

SELECT * FROM users WHERE upper() LIKE'%TESTSEARCH%'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'%TESTSEARCH%'' at line 1

big_k105 Mar 26th, 2008 2:59 PM

Re: Simple PHP Search Script
 
The problem is in your select statement. You don't have a column name after the WHERE in your select statement. Probably should look like this
:

  1. SELECT * FROM users WHERE info LIKE '%searchTerm%'


so in your php it should be:

:

  1. $sql = "SELECT * FROM users WHERE info LIKE '%$find%'";



All times are GMT -5. The time now is 2:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC