当我打开我的搜索页面时,
(1)它立即显示所有结果(我的数据库中的每个条目。)
(2) 当搜索按钮被空输入点击时,它还会显示我的数据库中的每个条目。
我该如何解决这个问题?任何帮助将非常感激。
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" value="search" />
</form>
<hr color="white">
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM table WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("localhost", "abc", "pass");
mysql_select_db("mydb");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$link = $row['link'];
$picture = $row['picture'];
$keywords = $row['keywords'];
$date = $row['date'];
echo $row['title'];
}
}
else
echo "No results found for \"<b>$k</b>\"";
//disconnect
mysql_close();
?>