Jump to content

Archived

This topic is now archived and is closed to further replies.

takeiteasy

Search Error

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]

Share this post


Link to post
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]

Share this post


Link to post
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..

Share this post


Link to post
Share on other sites

×

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.