用php+mysql 写搜索的问题

来源:百度知道 编辑:UC知道 时间:2024/06/18 06:45:13
有以下三个下拉菜单,选择后点击搜索,搜索出数据库中符合条件的信息,我的问题是,如何实现:假如其中有选项为“请选择...”(value为“se”时),则表示该项不搜索,搜索结果为符合其他项的结果。
(例如:选择:大连,白色,价格选择“请选择价格..”,搜索结果为“大连”和“白色”所有价格的结果)如何实现?谢谢 !
<select name="city" class="select">
<option value="se">请选择城市...</option>
<option value="大连">大连</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
</select>

<select name="c_color" class="select">
<option value="se">请选择颜色...</option>
<option value="手动">白色</option>
<option value="自动">黑色</option>
<option value="半自动">红色</option>
</select>

<select name="price" class="select">
<option value="se">请选择价格...</option>
<option value="1"&g

可以用if判断一下,假设你的三个字段分别为:city,c_color,price,数据库表名为tablename
if($_GET['city']=="se")
$search="";
if($_GET['city']<>""&&$_GET['city']<>"se")
$search="where city = '".$_GET['city']."'";
查询语句可以用:
"select * from tablename".$search;

其它的就不写了。

改进一下一楼的代码实现:
假设使用PHP代码

$values="city,c_color,price"; //这里放网页中的变量名,用逗号分开
$list=explode(",",$values);
$where=" where 1 ";
foreach($list as $key)
{
if($_REQUEST[$key]!='' && $_REQUEST[$key]!='se')
{
$where.=sprintf(" and %s='%s'",$key,$_REQUEST[$key]);
}
}

$sql="select * from table_name ".$where;
下面的代码自己去写咯。。