paulmo Posted January 13, 2009 Share Posted January 13, 2009 trying about.com search box script on any keyword, getting Notice: Undefined variable: searching in (line #) Notice: Undefined variable: find in (line #) seems those variables are defined in code below though. any help appreciated. (i've xxx'd out db connection for posting here): <form name="search" method="post" action="beta.php"> //posting to self Write something: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="terms">Terms</option> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <? //This is only displayed if they have submitted the form if ($searching =="yes") { echo "Results"; } //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database mysql_connect("xxx") or die(mysql_error()); mysql_select_db("xxx") or die(mysql_error()); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT * FROM db WHERE upper($field) LIKE'%$find%'"); //And we display the results while($result = mysql_fetch_array( $data )) { echo $result['terms']; echo " "; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/ Share on other sites More sharing options...
ngreenwood6 Posted January 13, 2009 Share Posted January 13, 2009 Actually neither one of those values are actually declared. if you were to put $searching = "yes"; above: //This is only displayed if they have submitted the form if ($searching =="yes") The error would go away. You form does not post to that page it posts to another page so the variable searching is not set a value. Same thing with $find. Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736313 Share on other sites More sharing options...
paulmo Posted January 13, 2009 Author Share Posted January 13, 2009 thanks! beta.php is the same page though; i'm trying to post page to itself. i've seen action " " as posting to same page, is this what i should do? Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736343 Share on other sites More sharing options...
ngreenwood6 Posted January 13, 2009 Share Posted January 13, 2009 you can post a page to itself but you still didnt declare it. you can declare it like this: $searching = $_POST['searching']; the post searching came from the name of the input on your form. The $find variable is still not declared though and it has no variable from the form. Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736364 Share on other sites More sharing options...
paulmo Posted January 14, 2009 Author Share Posted January 14, 2009 thanks for your help. the search box is only executing from single word search box entries. upon a match, the correct table field row is being echoed, but i need to have an entire sentence searched against table field. please take a look: <form name="search" method="post" action="beta.php"> <center>Write something: </br></br> <input type="text" name="find" class="buttonsb"/> <input type="hidden" name="searching" value="yes" /></br></br></br> <input type="submit" name="search" value="Process" class="buttons" /> </center> </form> $searching = $_POST['searching']; $searching = "yes"; if ($searching =="yes") { //echo "Results"; } //If they did not enter a search term we give them an error $find = $_POST['find']; //$find = "yes"; (if i leave $find = "yes" then the form echoes "yes" instead of matched text, ...was trying to emulate your suggestion with $searching = "yes") if ($find == "") { echo "Please write something."; exit; } // Otherwise we connect to our Database mysql_connect("xxx", "xxxx", "xxx) or die(mysql_error()); mysql_select_db("xxx") or die(mysql_error()); // We perform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT * FROM beta WHERE terms LIKE'%$find%'"); WHERE terms (and $result['terms'] below) refers to table field with text; this part is working //And we display the results while($result = mysql_fetch_array($data)) { echo $result['terms']; echo " "; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "try again<br><br> "; //And we remind them what they searched for echo "You wrote: " .$find; } i'll also need to insert the orginal user 'find' into a table field. seems only one possible query to a page. advice? //mysql_query("INSERT INTO beta (find, created) VALUES ('$find', NOW())")or die(mysql_error()); ?> thanks for help. remember i need 1) multiple word user search box match (not just single word) and 2) insert original 'find' variable (text) into a table field. Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736642 Share on other sites More sharing options...
ngreenwood6 Posted January 14, 2009 Share Posted January 14, 2009 First thing I see is that you need to remove the $searching = "yes". That was just to show you that you did not set the value and how to set it. When you do the $searching = $_POST['searching'] that sets the $searching. That also explains why the $find did not work for. You set it correctly doing $find = $_POST['find']. As far as only matching for the word, it should be working correctly the way you have it. check this out http://www.htmlite.com/mysql011.php. Basically the % at the end tells it to check for anything after the $find. The last statement that you made is incorrect. You can have multiple queries on a page. Actually it is more common than you would think to have multiple queries on one page. If it is not working tell me if you get an error or what happens. I don't understand how you are trying to enter that data in the same table. I would create a separte table for the searched words if I was you. Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736728 Share on other sites More sharing options...
paulmo Posted January 14, 2009 Author Share Posted January 14, 2009 As far as only matching for the word, it should be working correctly the way you have it. yes it's matching fine for one word, but not working at all for more multiple words. for example, "yellow" in search box echoes a table field row about a yellow sun, etc. but "yellow dog in the road" in search box echoes "try again." i've read the link you provided. possible fixes to this problem? create a separte table for the searched words ok so 2 separate database connections/queries for 2 different tables on same page? thanks again Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736750 Share on other sites More sharing options...
ngreenwood6 Posted January 14, 2009 Share Posted January 14, 2009 I meant to say that it will match the phrase. If you enter yellow sun but there is no entry that says yellow sun then it isn't going to find it because it is trying to find the phrase yellow sun. I do not know what you would search for to find any word in a search box. You are better off to post just that question in the mysql forums as they will be better with the queries. As far as the 2 queries goes. You do not need 2 connections for the queries. You only need 1 connection to the database with 2 different queries. For example $conn = mysql_connect("host","username","password"); $select_db = mysql_select_db("database"); $first_query = "SELECT * FROM table"; $first_results = mysql_query($first_query); $second_query = "INSERT INTO table2 (first_name,last_name) VALUES ('john','doe')"; $second_results = mysql_query($second_query); You do not need to duplicate the connection as long as you are using the same database. If you need to connect to 2 different databases then you would have to connect to that database but there is generally no need for separate databases just tables. you may want to look into database normalization. Basically what that is, is never having 2 of the same values in any table at any time. You will create separate tables with linking values to separate the data. It makes it much more efficient and overall better. Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736755 Share on other sites More sharing options...
paulmo Posted January 14, 2009 Author Share Posted January 14, 2009 thanks so much for that clarification on 2 queries. i'll try the mysql forum about the multiple search words. regards Quote Link to comment https://forums.phpfreaks.com/topic/140658-search-box-script/#findComment-736952 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.