我有一个使用PHP的搜索功能,提供按关键字搜索或其他类别搜索的选项。举例说明:
Search by keyword: __________
Search by the following (drop down menus):
1. Author
2. Category
3. Theme
4. Region
我遇到的许多错误之一是,如果用户在关键字搜索字段中键入内容,然后从下拉菜单中选择一个类别,则搜索查询不成功(0结果)。无论用户输入什么或从下拉菜单中选择什么,我该怎么做才能让它运行?
结果页面的代码如下。提前感谢您提供任何帮助!:
连接:
<?php
$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' . 'database at this time.</p>');
}
$authors = @mysql_query('SELECT id, name FROM author');
if (!$authors) {
exit('<p>Unable to obtain author list from the database.</p>');
}
$cats = @mysql_query('SELECT id, name FROM category');
if (!$cats) {
exit( '<p>Unable to obtain category list from the database.</p>');
}
$themes = @mysql_query('SELECT id, name FROM theme');
if (!$themes) {
exit( '<p>Unable to obtain category list from the database.</p>');
}
$geofoci = @mysql_query('SELECT id, name FROM geofocus');
if (!$geofoci) {
exit( '<p>Unable to obtain category list from the database.</p>');
}
?>
实际形式:
<form class="searchField" name="input" action="fundfetch_search.php" method="post">
<ul>
<li>
<label>Search by keyword:</label>
<input type="text" name="searchtext" class="styleSearchbox" placeholder="By keyword" value="<?php echo $_POST['searchtext']; ?>">
</li>
<li>
<label>OR by the following: </label>
<label><select name="aid" size="1" class="styleDropdown">
<option selected value="">Any Author</option>
<?php
while ($author = mysql_fetch_array($authors)) {
$aid = $author['id'];
$aname = htmlspecialchars($author['name']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="cid" size="1" class="styleDropdown">
<option selected value="">Any Category</option>
<?php
while ($cat = mysql_fetch_array($cats)) {
$cid = $cat['id'];
$cname = htmlspecialchars($cat['name']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="tid" size="1" class="styleDropdown">
<option selected value="">Any Theme</option>
<?php
while ($theme = mysql_fetch_array($themes)) {
$tid = $theme['id'];
$tname = htmlspecialchars($theme['name']);
echo "<option value='$tid'>$tname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="gfid" size="1" class="styleDropdown">
<option selected value="">Any Region</option>
<?php
while ($geofocus = mysql_fetch_array($geofoci)) {
$gfid = $geofocus['id'];
$gfname = htmlspecialchars($geofocus['name']);
echo "<option value='$gfid'>$gfname</option>\n";
}
?>
</select></label>
</li>
<li style="visibility:hidden"><a href="../FUNDER.COM website/searchfilteroption">Closing</a></li>
<li><input type="submit" value="Search" class="searchButton"></li>
</ul>
</form>
数据库查询:
<?php
$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' . 'database at this time.</p>');
}
// The basic SELECT statement
$select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate,
author.id AS author_id, author.name AS author_name,
jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, category.id AS cat_id, category.name as cat_name,
joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id AS theme_id, theme.name AS theme_name,
jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid, geofocus.id AS geofocus_id, geofocus.name AS geofocus_name';
$from = ' FROM joke, author, jokecategory, category, joketheme, theme, jokegeofocus, geofocus';
$where = ' WHERE joke.authorid = author.id AND joke.id = jokecategory.jokeid AND jokecategory.categoryid = category.id AND joke.id = joketheme.jokeid AND joketheme.themeid = theme.id AND joke.id = jokegeofocus.jokeid AND jokegeofocus.geofocusid = geofocus.id';
$in = ' ORDER BY jokedate DESC';
$aid = $_POST['aid'];
if ($aid != '') { // An author is selected
$where .= " AND authorid='$aid'";
}
$cid = $_POST['cid'];
if ($cid != '') { // A category is selected
$from .= ''; // usually written as ' ,tablename'
$where .= " AND joke.id=jokecategory.jokeid AND categoryid='$cid'";
}
$tid = $_POST['tid'];
if ($tid != '') { // A theme is selected
$from .= '';
$where .= " AND joke.id=joketheme.jokeid AND themeid='$tid'";
}
$gfid = $_POST['gfid'];
if ($gfid != '') { // A region is selected
$from .= '';
$where .= " AND joke.id=jokegeofocus.jokeid AND geofocusid='$gfid'";
}
$searchtext = $_POST['searchtext'];
if ($searchtext != '') { // Some search text was specified
$where .= " AND keywords LIKE '%$searchtext%'";
}
?>
结果:
<?php
$jokes = @mysql_query($select . $from . $where . $in);
if (!$jokes) {
echo '</table>'; exit('<p>Error retrieving jokes from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}
$numrows = mysql_num_rows($jokes);
if ($numrows>0){
while ($joke = mysql_fetch_array($jokes)) {
$id = $joke['id'];
$joketext = htmlspecialchars($joke['joketext']);
$jokedate = htmlspecialchars($joke['jokedate']);
$aname = htmlspecialchars($joke['author_name']);
$category = htmlspecialchars($joke['cat_name']);
$theme = htmlspecialchars($joke['theme_name']);
$geofocus = htmlspecialchars($joke['geofocus_name']);
$position = 200;
$post = substr($joketext, 0, $position);
echo "<li id=\"jump\">
<article class=\"entry\">
<header>
<h3 class=\"entry-title\"><a href=''>$aname</a></h3>
</header>
<div class=\"entry-content\">
<p>$post...</p>
</div>
<div class =\"entry-attributes\">
<p>> Category: $category</p>
<p> > Theme(s): $theme</p>
<p> > Region(s) of focus: $geofocus</p>
</div>
<footer class=\"entry-info\">
<abbr class=\"published\">$jokedate</abbr>
</footer>
</article>
</li>";
}
}
else
echo "Sorry, no results were found. Please change your search parameters and try again!";
?>
答案 0 :(得分:1)
我甚至不知道从哪里开始...
首先,永远不会推动您的用户,请清理您的数据库输入。
第二次,代码被写入供人们理解,你在查询中丢失了很多重复项,我认为因为查询难以阅读,示例AND joke.id = jokecategory.jokeid
出现了2次。
更多重新发布:
inner join
与on
一起使用,使查询更具可读性见下面的代码:
$select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate,
author.id AS author_id, author.name AS author_name,
jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid,
category.id AS cat_id, category.name as cat_name,
joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id
AS theme_id, theme.name AS theme_name,
jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid,
geofocus.id AS geofocus_id, geofocus.name AS geofocus_name';
$from = ' FROM joke
inner join author on (joke.authorid = author.id)
inner join jokecategory on (joke.id = jokecategory.jokeid)
inner join category on (jokecategory.categoryid = category.id)
inner join joketheme on (joke.id = joketheme.jokeid)
inner join theme on (joketheme.themeid = theme.id)
inner join jokegeofocus on (joke.id = jokegeofocus.jokeid)
inner join geofocus on (jokegeofocus.geofocusid = geofocus.id)';
$first_where = ' where ';
$where = '';
$in = ' ORDER BY jokedate DESC';
if (is_numeric($_POST['aid']))
{ // An author is selected
$where.= $first_where.' authorid='.$_POST['aid'];
$first_where = ' and ';
}
if (is_numeric($_POST['cid']))
{ // A category is selected
$where.= $first_where.' categoryid='.$_POST['cid'];
$first_where = ' and ';
}
if (is_numeric($_POST['tid']))
{ // A theme is selected
$where.= $first_where.' themeid='.$_POST['tid'];
$first_where = ' and ';
}
if (is_numeric($_POST['gfid']))
{ // A region is selected
$where.= $first_where.' geofocusid='.$_POST['gfid'];
$first_where = ' and ';
}
if (isset($_POST['searchtext']) and $_POST['searchtext'] != '')
{ // Some search text was specified
$where.= $first_where.' keywords LIKE "%'.(mysql_real_escape_string($_POST['searchtext'], $dbcnx)).'%"';
}
if($where == '')
{ // prevents returning the whole database, if form is empty
$where = ' limit 20';
}
如果您仍有问题(我没有时间对其进行测试),请测试我的代码并发表评论(将其添加到您的答案中,您没有真正意见的声誉)。
答案 1 :(得分:0)
我建议显示混合结果,其中关键字存在于该类别中,然后可能会列出其他类别以及找到该关键字的计数。想想像亚马逊这样的大型网站。如果我从类别列表中选择“电影”并输入“迪士尼”,我会看到与迪士尼相匹配的电影中最受欢迎的结果。然后在左边,我有更多的选择来缩小或扩大搜索子/其他类别。