Hi people,
I have been trying to develop a database searching PHP script and encountered some problem. The problem is that whatever I type in as the keyword the script would return me all the entries from the database regardless of whether the word is actually there or not. Can someone help me please? Below is how I wrote the search script.
<?
//database connection
include 'includes/database.php';
//search query
$sql=mysql_query("SELECT * FROM stories WHERE title LIKE '%".$_POST['keyword']."%'
OR author LIKE '%".$_POST['keyword']."%' OR content LIKE '%".$_POST['keyword']."%'");
$num_results=mysql_num_rows($sql);
//table and loop for displaying results
echo 'You have searched for: '.$keyword.'<p>Number of Results: '.$num_results.'</p>';
echo '<table width="504" border="0" valign="top" cellspacing="0" cellpadding="0">';
echo '<tr class="style1">';
echo '<td width="200"><span class="style4">Title</span></td>';
echo '<td width="100"><strong>Author</strong></td>';
echo '<td width="30"><strong>Vol</strong></td>';
echo '<td width="30"><strong>No</strong></td>';
echo '<td><strong>Intro</strong></td>';
echo '</tr>';
for ($i=0; $i<$num_results; $i++){
$row=mysql_fetch_array($sql);
$id=$row['storynum'];
$t=$row['title'];
$au=$row['author'];
$vol=$row['volume'];
$num=$row['number'];
$fp=$row['first_p'];
echo '<tr>';
echo '<td><a href="article.php?id='.$id.'">'.$t.'</a></td>';
echo '<td>'.$au.'</td>';
echo '<td>'.$vol.'</td>';
echo '<td>'.$num.'</td>';
echo '<td>'.$fp.'</td>';
echo '</tr>';
}
echo '</table>';
?>