我有这个代码。但问题在于它仅显示基于上次选中的复选框的结果。我想在选中两个或多个复选框时显示结果。我不知道缺少什么。单击提交按钮时,我会过滤结果。
$sql = "SELECT * FROM table WHERE type ='A'";
if(isset($_POST['filter'])){
$location = mysqli_real_escape_string($connection,$_POST['location']);
if(empty($_POST['price'])){
$sql = "SELECT * FROM table WHERE type ='A' AND location LIKE '%".$location."%'";
}else{
foreach($_POST['price'] as $price){
$sql = "SELECT * FROM table WHERE type ='A' AND location LIKE '%".$location."%' AND price IN ('$price')";
}
}
}
$query = mysqli_query($connection,$sql) or die(mysqli_error());
这是我为其创建的html。我没有介绍结果的显示方式。我发现这与问题无关。
<form name="location form" method="POST"">
Filter by Location: <input type="text" name="location" placeholder=" " value=""/><br><br>
<label><strong>Price Range:</strong></label>
<br>
<input class="form-check-input" name="price[]" type="checkbox" value="s 500 - s 1000">$500-$1000<br>
<input class="form-check-input" name="price[]" type="checkbox" value="s 2500 - s 4000">$2500-$4000<br>
<input class="form-check-input" name="price[]" type="checkbox" value="s 5000 - s 9000">$5000-$9000<br>
<input class="form-check-input" name="price[]" type="checkbox" value="s 10000 - s 50000">$10000-$50000<br>
<input type="submit" name="filter" value="Filter" class="btn btn-primary" >
</form>
任何帮助将不胜感激,并祝您愉快!
更新 当我选中所有复选框时,这是我回显它时的输出 s 500-s 1000s 2500-s 4000s 5000-s 9000s 10000-s 50000
答案 0 :(得分:0)
解决了。我先将元素包装在引号中,然后将其内插到查询中。谢谢你们的帮助。
$finalprice = array();
foreach($_POST['price'] as $key=>$price){
$finalprice[] = "'" . $price . "'";
$sql = "SELECT * FROM table WHERE type ='A' AND location LIKE '%".$location."%' AND price IN (".implode(',',$finalprice).")";
}
答案 1 :(得分:-2)
尝试这个;)
$sql = "SELECT * FROM table WHERE type ='A'";
if(isset($_POST['filter'])){
$location = mysqli_real_escape_string($connection,$_POST['location']);
if(empty($_POST['price'])){
$sql = "SELECT * FROM table WHERE type ='A' AND location LIKE '%".$location."%'";
}else{
foreach($_POST['price'] as $price){
if($price == 500){
$sql = "SELECT * FROM table WHERE type = 'A' AND location LIKE '%".$location."%' AND price >= '".$price."' AND price = '".$price."' AND price = '".$price."' AND price = '".$price."' AND price
<form name="location form" method="POST"">
Filter by Location: <input type="text" name="location" placeholder=" " value=""/><br><br>
<label><strong>Price Range:</strong></label>
<br>
<input class="form-check-input" name="price[]" type="checkbox" value="500">$500-$1000<br>
<input class="form-check-input" name="price[]" type="checkbox" value="2500">$2500-$4000<br>
<input class="form-check-input" name="price[]" type="checkbox" value="5000">$5000-$9000<br>
<input class="form-check-input" name="price[]" type="checkbox" value="10000">$10000-$50000<br>
<input type="submit" name="filter" value="Filter" class="btn btn-primary" >
</form>