takeiteasy Posted May 18, 2006 Share Posted May 18, 2006 I have a problem with the search function here, when i entered a valid workerName which is in the database called archive, it returned Record Found.But when i entered an invalid workerName, it also display Record Found.I think my code doesn't check with my database if there's simliar workerName, just display Record Found whenever i press Submit. Can someone help to see where my code goes wrong? TIA![code]<form method='post' action='<? echo $_SERVER[PHP_SELF]; ?>'><?if (isset($_POST['submit'])){$searchresults = mysql_query("SELECT * FROM archive WHERE workerName LIKE '%$_POST[search]%'", $db_connection) or die(mysql_error()); }if ($searchresults == true) { echo "Record Found";} ?><TABLE width='770' border='0' align='center' cellpadding='0' cellSpacing='0' style='border:1px solid #9FD2EC'> <TBODY> <TR> <TD height='30' align='center' valign='middle'>Search By Worker/Passenger Name:<input type='text' name='search' size='20'><input type='submit' name='submit' value='Submit'></td></table></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9903-search-error/ Share on other sites More sharing options...
alpine Posted May 18, 2006 Share Posted May 18, 2006 actually, you are asking if the query itself returns true or not and as long as the query runs ok it will return true in my sence. You can use mysql_num_rows to get the number of results.You also should put quotes within your $_POSTTry this[code]if (isset($_POST['submit'])){$searchresults = mysql_query("SELECT * FROM archive WHERE workerName LIKE '%$_POST['search']%'", $db_connection) or die(mysql_error());if (mysql_num_rows($searchresults) > 0){echo "Record Found";} }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9903-search-error/#findComment-36821 Share on other sites More sharing options...
takeiteasy Posted May 18, 2006 Author Share Posted May 18, 2006 Thanks alpine for replying.I've tried your solution, and it worked! Thanks alot!!and btw, i can't put quotes within $_POST if i do so, it will have this error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING , when i removed the quotes, everything worked fine.. Quote Link to comment https://forums.phpfreaks.com/topic/9903-search-error/#findComment-36827 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.