Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

php mysql advanced search select box and radio box issues

I want to add advanced search into my web page

I use this form for getting data

<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

I got data like this

$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'];

here is the sql query

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...