Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 26th, 2008, 1:18 PM   #1
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 3 Arnack is on a distinguished road
Simple PHP Search Script

I put this all on index.php:
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 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?
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.

Last edited by big_k105; Mar 26th, 2008 at 1:23 PM. Reason: changed ICODE tag to CODE=PHP tag
Arnack is offline   Reply With Quote
Old Mar 26th, 2008, 1:26 PM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,613
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
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:

php Syntax (Toggle Plain Text)
  1. $searching = $_POST['searching'];

Put that above the if statement and it will work.
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Mar 26th, 2008, 1:31 PM   #3
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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

php Syntax (Toggle Plain Text)
  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
Freaky Chris is offline   Reply With Quote
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
Old Mar 26th, 2008, 1:58 PM   #5
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,613
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Re: Simple PHP Search Script

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. <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");
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Mar 26th, 2008, 2:20 PM   #6
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 3 Arnack is on a distinguished road
Re: Simple PHP Search Script

Alright it does print out Query Error..
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Mar 26th, 2008, 2:39 PM   #7
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,613
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
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:
php Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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.
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Mar 26th, 2008, 2:41 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,788
Rep Power: 5 Sane will become famous soon enough
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.

php Syntax (Toggle Plain Text)
  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();
Sane is online now   Reply With Quote
Old Mar 26th, 2008, 2:45 PM   #9
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 3 Arnack is on a distinguished road
Re: Simple PHP Search Script

Okay. Here is my MySQL code:

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" )
I don't see anything wrong here.. hmm..
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 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
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Mar 26th, 2008, 2:59 PM   #10
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,613
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
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
sql Syntax (Toggle Plain Text)
  1. SELECT * FROM users WHERE info LIKE '%searchTerm%'

so in your php it should be:

php Syntax (Toggle Plain Text)
  1. $sql = "SELECT * FROM users WHERE info LIKE '%$find%'";
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump