我正在尝试根据多个复选框进行查询。(适用于电子商店)
复选框
<div id="car">
<label><input type="checkbox" value="red" >red</label>
<label><input type="checkbox" value="black" >black</label>
<label><input type="checkbox" value="blue" >blue</label>
</div>
<div id="brand">
<label><input type="checkbox" value="mazda" >mazda</label>
<label><input type="checkbox" value="bmw" >bmw</label>
</div>
查询
$brand = $_POST['brand'];
$color = $_POST['color'];
$Query = "SELECT * FROM items WHERE brand IN ('" . implode("','", $brand) . "') and color IN ('" . implode("','", $color) . "')";
$QueryResult = mysqli_query($connection , $Query);
while($QueryRow = mysqli_fetch_assoc($QueryResult)){
?>
<div><p><?php echo $QueryRow['cost'];?></p></div>
<div><img src="<?php echo $QueryRow['img']; ?>"></div>
<?php
}
}
?>
另外,我有js脚本用于传递带有复选框值的数组,但它与问题没有关系,这就是我不发布脚本的原因。
问题:
如果未选中COLOR复选框,如何避免错误?
有什么想法吗?
答案 0 :(得分:0)
"SELECT * FROM items WHERE brand IN ('" . implode("','", $brand) . "') and color IN ('" . implode("','", $color) . "')";