php mysql 高级搜索选择框和单选框问题 [英] php mysql advanced search select box and radio box issues

查看:33
本文介绍了php mysql 高级搜索选择框和单选框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add advanced search into my web page

我使用此表单获取数据

<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>

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

I got some fields from database, anyway I successfully post form and get data but my search results are wrong

我得到了这样的数据

$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."%'    ");

那么,我该如何进行这样的高级搜索?

So, how can I do advanced search like this?

推荐答案

由于最终的 OR 运算符限定了所有包含 descriptions 的记录,因此您的查询返回了错误的结果代码>$oneword.相反,您需要使用 () 将文本搜索条件组合在一起.

Your query was returning incorrect result because of the final OR operator qualified all records with descriptions containing $oneword. Instead, you need to use () to group the text search conditions together.

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."%');

这篇关于php mysql 高级搜索选择框和单选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆