Home  >  Article  >  Backend Development  >  多条件查询,提交的空值(条件)怎么处理

多条件查询,提交的空值(条件)怎么处理

WBOY
WBOYOriginal
2016-06-23 14:26:031052browse

表1里有品牌,年限,车型
board(BMW,ford,aodi)
year(2011,2012,2013)
type(car,suv,mpv)

我做了三个下拉菜单,用户搭配选择查询
1,选了board里的BMW和year里的2013,但第三个type没选,

但在做的时候type字段就会发送空,查询时就等于
board='BMW'AND year="2013" AND type='' 
我要实现 不选择的字段IS NOT NULL就是BMW和2013的所有车型,
board='BMW'AND year="2013" AND type is not null

或者

board='BMW'AND year="2013"
这个怎么做,


回复讨论(解决方案)

可以参考一下写法

$where = array();if($board){    $where[]=" board='$board'";}if($year){    $where[]=" year='$year'";}....$sql.=(!empry($where)) ? ' where ' . implode(' and ', $where) : '';

设提交后 $_POST 为
$_POST = array('board' => 'BMW', 'year' => '2013', 'type' => '');

$w = array();
foreach($_POST as $k=>$v) {
  if(! empty($v)) $w[] = "$k='$v'";
}
$where = join(' and ', $w);
得到
board='BMW' and year='2013'

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn