【问题标题】:php mysql advanced search select box and radio box issuesphp mysql 高级搜索选择框和单选框问题
【发布时间】:2014-06-14 16:55:22
【问题描述】:

我想在我的网页中添加高级搜索

我用这个表格来获取数据

<form action="advsearch.php" method="POST" >

Search <input type="text" name="oneword" size="20"> word

<p>Time <input type="radio" value="30" name="time">30 min 
    az <input type="radio" name="time" value="60">60 min
    <input type="radio" name="time" value="120">120 min
    <input type="radio" name="time" value="1000" checked>any</p>
<p>&nbsp;</p>
</p>

Cities 

<?php

while($cities=mysql_fetch_array($whichcity)){
echo "<input type='checkbox' name='cities[]' value='$cities[cityname]'>$cities[cityname]";
}
?>

    <p>Category

<?php

while($categories=mysql_fetch_array($whichcat)){
echo "<input type='checkbox' name='categories[]' value='$categories[catname]'>$categories[catname]";
}
?>

    </p>
<p>Weather <input type="checkbox" name="weather[]" value="Rainy">Rainy
    <input type="checkbox" name="weather[]" value="Cloudy">Cloudy
    <input type="checkbox" name="weather[]" value="Clear">Clear
    <input type="checkbox" name="weather[]" value="Foggy">Foggy</p>
<p>&nbsp;Person<input type="radio" name="person" value="2">1-2 person<input type="radio" name="person" value="4">3-4 
    person<input type="radio" name="person" value="6">5-6 person<input type="radio" name="person" value="100" checked>any

    </p>

    <p><input type="submit" value="Search"></p>


</form>

我从数据库中获取了一些字段,无论如何我成功发布表单并获取数据但我的搜索结果错误

我得到了这样的数据

$oneword = mysql_real_escape_string($_POST['oneword']); 
$time = (int)$_POST['time'];
$cities= implode(",",$_POST["cities"]);
$categories= implode(",",$_POST["categories"]);
$weather= implode(",",$_POST["weather"]);
$person= (int)$_POST['person'];

这里是sql查询

$sql= mysql_query("select * from mytable where time <= '".$time ."' and person = '".$person."' and  category like '".$categories."' and  cities like '".$cities."' and  weather like '".$weather."' and word like '%".$oneword."%' or description like '%".$oneword."%'    ");

那么,我怎样才能进行这样的高级搜索呢?

【问题讨论】:

  • 这是错误的$weather= weather(",",$_POST["weather"]);
  • 是的,这是我的错误,我已经修复了
  • 你有name="oneword",但使用($_POST['word '])另外,['word ']中的空间也会导致问题。
  • ['time '] 内部还有一个空格,这也会给你带来麻烦。
  • 在开发过程中,回显 $sql; 总是有用的

标签: php mysql advanced-search


【解决方案1】:

您的查询返回了不正确的结果,因为最终的 OR 运算符限定了包含 $oneworddescriptions 的所有记录。相反,您需要使用() 将文本搜索条件组合在一起。

select * 
from mytable 
where time <= '".$time ."' and person = '".$person."' 
  and  category in ('". implode("','", $_POST['categories']) ."') 
  and  cities in ('". implode("','", $_POST['cities']) ."')
  and  weather in ('". implode("','", $_POST["weather"]) ."') 
  and (word like '%".$oneword."%' or description like '%".$oneword."%');

【讨论】:

  • 另外,LIKE 运算符接受一个带有通配符的字符串。您拥有的是逗号分隔的列表。请改用IN 函数,例如。 weather IN (" . $weather . ")".
  • @NisseEngström,说得好。答案已根据您的建议更新。
  • 但每次搜索都没有鱼子回报
  • @Seyhan,你能在发送到mysql_query之前打印查询吗?
  • 实际上我发现了不同的问题,如果我不选择选择框的任何选项,则没有行返回。如果没有选择,我如何搜索所有?
猜你喜欢
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
相关资源
最近更新 更多