我不断收到这个简单脚本的空查询错误。有任何想法吗?
我只有一个带有输入框和按钮的表单,在表单之外我有一个名为 filter1 的下拉列表,其中包含 3 个值“整个站点”、“页面”、“博客”。
<?php
//process the search query
if (isset($_POST['submitted'])) {
require('db_conn.php'); // connect to db
$search_query = $_POST['searchquery'];
// check filter
if ($_POST['filter1'] == 'Whole Site') {
$q = "(SELECT id, page_title AS title FROM pages WHERE page_title LIKE '%$searchquery%' OR page_body LIKE '%$searchquery%') UNION (SELECT id, blog_title AS title FROM blog WHERE blog_title LIKE '%$searchquery%' OR blog_body LIKE '%$searchquery%')";
}
else if ($_POST['filter1'] == 'Pages') {
$q = "SELECT id, page_title AS title FROM pages WHERE page_title LIKE '%$searchquery%' OR page_body LIKE '%$searchquery%'";
}
else if ($_POST['filter1'] == 'Blog') {
$q = "SELECT id, blog_title AS title FROM blog WHERE blog_title LIKE '%$searchquery%' OR blog_body LIKE '%$searchquery%'";
}
$r = mysqli_query($dbc, $q); // query the db
$count = mysqli_num_rows($r);
if ($count > 1) {
$search_output = 'ok';
}
else {
$search_output = "<hr />0 results for <strong>$search_query</strong><hr />$q";
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h2>Search the tables</h2>
<form action="search.php" method="post">
<p>Search: <input type="text" name="searchquery"/></p>
<p><input type="submit" name="submit" value="Search"/></p>
<input type="hidden" name="submitted" value="TRUE"/>
</form>
<br/>
Search in:
<select name="filter1">
<option value="Whole Site">Whole site</option>
<option value="Pages">Pages</option>
<option value="BLog">Blog</option>
</select>
<?php echo $search_output; ?>
</body>
</html>