2014-06-14 79 views
-1

我要添加高級搜索到我的網頁PHP MySQL的高級搜索選擇框,單選框發出

我用這種形式獲取數據

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

那麼,我該如何做高級搜索呢?

+0

這是錯誤的'$ weather = weather(「,」,$ _ POST [「weather」]); ' –

+0

是的,這是我的錯誤,我修正了它 – Seyhan

+0

你有'name ='oneword''但與'($ _POST ['word'])一起使用'''''''word']'問題。 –

回答

2

由於最終的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."%'); 
+0

另外,'LIKE'運算符采用帶有通配符的字符串。你有什麼是逗號分隔的列表。使用'IN'函數,例如。天氣IN(「。$ weather。」)「'。 –

+0

@NisseEngström,good point。answer根據您的建議更新了 – Fabricator

+0

但是這不是每次搜索都會收到回覆 – Seyhan