Jump to content


Photo

Search Error


  • Please log in to reply
2 replies to this topic

#1 takeiteasy

takeiteasy
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 18 May 2006 - 04:57 AM

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!
<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>


#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 18 May 2006 - 07:22 AM

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 $_POST

Try this
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";
} 
}


#3 takeiteasy

takeiteasy
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 18 May 2006 - 08:15 AM

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..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users