xcali Posted April 4, 2008 Share Posted April 4, 2008 Hello dear php members! I have a problem with a script that searches the database for users. After user types in the text, the results page doesn't show up for some reason. My current code is: <? if ($searching =="yes") { echo "<h2>Results</h2><p>"; if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx") or die(mysql_error()); mysql_select_db("xxxxx") or die(mysql_error()); $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); while($result = mysql_fetch_array( $data )) { echo $result['first_name']; echo " "; echo $result['last_name']; echo "<br>"; echo "<br>"; } $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } echo "<b>Searched For:</b> " .$find; } ?> <h2>Search</h2> <form name="search" method="post" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="first_name">First Name</option> <Option VALUE="last_name">Last Name</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> may someone please guide me what i did wrong? Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/ Share on other sites More sharing options...
DyslexicDog Posted April 5, 2008 Share Posted April 5, 2008 Where does the $field variable get set? Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509689 Share on other sites More sharing options...
DyslexicDog Posted April 5, 2008 Share Posted April 5, 2008 hmmm can't edit.... I looked a little deeper at your code. You should turn on your error reporting to start with for testing. Give this code a try see if kicks any errors back. <?php if ($_POST['searching'] == "yes") { echo "<h2>Results</h2><p>"; if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx") or die(mysql_error()); mysql_select_db("xxxxx") or die(mysql_error()); $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); while($result = mysql_fetch_array( $data )) { echo $result['first_name']; echo " "; echo $result['last_name']; echo "<br>"; echo "<br>"; } $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } echo "<b>Searched For:</b> " .$find; } ?> <h2>Search</h2> <form name="search" method="post" action="<?=$_SERVER['PHP_SELF']?>"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="first_name">First Name</option> <Option VALUE="last_name">Last Name</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509696 Share on other sites More sharing options...
xcali Posted April 5, 2008 Author Share Posted April 5, 2008 thank you very much for your reply i did run the code each time i do - it comes back with: "Results You forgot to enter a search term" even though i did eneter criteria : / still wondering what could be wrong with it :s Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509729 Share on other sites More sharing options...
benjaminbeazy Posted April 5, 2008 Share Posted April 5, 2008 change if ($find == "") to if ($_POST['find'] == "") Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509781 Share on other sites More sharing options...
xcali Posted April 5, 2008 Author Share Posted April 5, 2008 uuuu getting somewhere so now if i dont enter anything it says "You forgot to enter a search term" which is good but then whatever else i enter it says "Sorry, but we can not find an entry to match your query" - no matter if it does exist in the database or not :/ so far thank you everyone for helping out ! Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509814 Share on other sites More sharing options...
benjaminbeazy Posted April 5, 2008 Share Posted April 5, 2008 try this... <?php if ($_POST['searching'] == "yes") { echo "<h2>Results</h2><p>"; $find = $_POST['find']; $field = $_POST['field']; if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx") or die(mysql_error()); mysql_select_db("xxxxx") or die(mysql_error()); $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); while($result = mysql_fetch_array( $data )) { echo $result['first_name']; echo " "; echo $result['last_name']; echo "<br>"; echo "<br>"; } $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } echo "<b>Searched For:</b> " .$find; } ?> <h2>Search</h2> <form name="search" method="post" action="<?=$_SERVER['PHP_SELF']?>"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="first_name">First Name</option> <Option VALUE="last_name">Last Name</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509816 Share on other sites More sharing options...
xcali Posted April 5, 2008 Author Share Posted April 5, 2008 nice !!! it has couple of bugs i need to change but it works well !! Thank you very much !!!!!!! Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509817 Share on other sites More sharing options...
benjaminbeazy Posted April 5, 2008 Share Posted April 5, 2008 no prob. remember whenever you post to a script through a form that you have to declare the variables as $var = $_POST['var'] or something similar Link to comment https://forums.phpfreaks.com/topic/99616-php-search-script-problem/#findComment-509818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.