PHP 多个输入框查询条件的拼接技巧
$username = $_POST['username'];
$age = $_POST['age'];
$uid = $_POST['uid'];
$address = $_POST['address'];
$where =" where 1=1 "; //1=1 是为了衔接and
if($username!=""){
$where .= " and username = '".$username."'";
}
if($age!=""){
$where .= " and age = '".$age."'";
}
if($uid!=""){
$where .= " and uid = '".$uid."'";
}
if($address!=""){
$where .= " and address= '".$address."'";
}
echo $where; //只要参数不为空,就会拼接到where上面作查询条件
输入框有内容就拼接上到查询条件语句上,这样就可以灵活地按不同的条件进行数据查询。
end!!!
注:如有缺陷或不足请反馈,我及时纠正,谢谢!!
笔记 2020-09-13