Jump to content

Search Error


takeiteasy

Recommended Posts

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]
Link to comment
Share on other sites

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
[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]
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.