简体   繁体   English

使用范围内的价格过滤产品

[英]Filter product using price in range

I am trying to make price filter for product, using checkbox. 我正在尝试使用复选框对产品进行价格过滤。 here is my filter 这是我的过滤器

0-500
501-1000
1001-2500
2501-5000

If i select one checkbox then filter work but when i select more than one checkbox give me error like: Notice : 如果我选择一个复选框,则过滤器工作,但是当我选择多个复选框时,出现如下错误:注意:

Trying to get property of non-object in D:\\xamp\\htdocs\\product\\filter_ledtv.php on line 11 尝试在第11行的D:\\ xamp \\ htdocs \\ product \\ filter_ledtv.php中获取非对象的属性

Below i provide code what i tryed, 我在下面提供了我尝试过的代码,
Here is my HTML Code 这是我的HTML代码

<div class="control-group">                     
<label class="control control--checkbox"> 0-500
  <input type="checkbox" value="0 AND 500" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>
<div class="control-group">                     
<label class="control control--checkbox"> 501-1000
  <input type="checkbox" value="501 AND 1000" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>
<div class="control-group">                     
<label class="control control--checkbox"> 1001-2500
  <input type="checkbox" value="1001 AND 2500" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>
<div class="control-group">                     
<label class="control control--checkbox"> 2501-5000
  <input type="checkbox" value="2501 AND 5000" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>

Here is my php code: 这是我的PHP代码:

<?php
$price="";
$price = isset($_REQUEST['price'])?$_REQUEST['price']:"";

$sql = "SELECT * FROM wm_ledtv WHERE pro_live='N'";
if(!empty($price)){
    $price =implode("'or pro_price between'",$price);
    $sql  .= " and pro_price between $price"; 
}
$result = $conn->query($sql);
if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        ?>
         fliter product show.............
        <?php
    }
}
?>

A few problems. 一些问题。

First, you don't have name="price[]" in your checkbox inputs. 首先,您的复选框输入中没有name="price[]"

Second, you're adding inappropriate quotes around the price ranges. 其次,您在价格范围附近添加了不合适的报价。 It should be: 它应该是:

$price =implode(" or pro_price between ",$price);

If you did echo $sql; 如果您确实echo $sql; you would have seen that the query looked wrong, you'd see: 您会发现查询看起来不对,您会看到:

WHERE pro_price BETWEEN 0 AND 500'or pro_price between'501 AND 1000

Your code is also vulnerable to SQL-injection. 您的代码也容易受到SQL注入的攻击。 It would be best if you used parametrized queries, but you should at least sanitize the inputs. 最好使用参数化查询,但至少应清理输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM