用于数据过滤的SQL查询

时间:2018-01-04 04:34:26

标签: php mysql sql

我有6个输入字段

select nopol, min(LASTSERVICE) LASTSERVICE, max(TGL) TGLREAL  
from TRREALPRWT 
group by nopol

每个字段用于过滤数据。如果所有字段都已填满,那么查询数据很容易,但我实际上并不知道用户要过滤多少个字段。他可以仅使用名称,名称和品牌名称,名称和品牌名称以及尺寸,价格和库存来过滤数据。使用<input type="text" class="form-control filter-width namef" placeholder="Product Name"> <input type="text" class="form-control filter-width brandf" placeholder="Brand Name"> <input type="text" class="form-control filter-width catf" placeholder="Category"> <input type="text" class="form-control filter-width sizef" placeholder="Size"> <input type="text" class="form-control filter-width pricef" placeholder="Price"> <input type="text" class="form-control filter-width invf" placeholder="Inventory"> 放置条件并考虑所有可能的组合将是困难和冗长的任务。 有没有办法实现这一目标。 这是我的PHP:

if, elseif

现在我不知道在$name = $_REQUEST['name']; $brand = $_REQUEST['brand']; $cat = $_REQUEST['cat']; $size = $_REQUEST['size']; $price = $_REQUEST['price']; $inv = $_REQUEST['inv']; if(!empty($name) AND !empty($brand) AND !empty($cat) AND !empty($size) AND !empty($price) AND !empty($inv) ||){ $sql = "SELECT * FROM products WHERE pname='$name' AND brand_name ='$brand' AND ptype = '$cat' AND psize= '$size' AND sprice = '$price' AND inventory='$inv'"; } else{ } $result = $conn->query($sql); if($result->num_rows>0){ while($row=$result->fetch_assoc()){ $pid = $row['pid']; $pname = $row['pname']; $pbrand = $row['brand_name']; $pcat = $row['ptype']; $pinv = $row['inventory']; $pprice = $row['sprice']; $psize = $row['psize']; ?> <tr id="<?php echo $pid; ?>" class="prod-details"><?php echo "<td>".$pid."</td><td>".$pname."</td><td>".$pbrand."</td>"."<td>".$pcat."</td>"."<td>".$psize."</td>"."<td>".$pprice."</td>"."<td>".$pinv."</td>"; ?></tr> <?php } } body

中思考和写作的条件

2 个答案:

答案 0 :(得分:2)

您可以汇总您的查询字符串。您可以尝试以下方法 -

$query = "";
    if (!empty($name)) {
            $query += " AND pname='$name'";
    }
    if (!empty($brand)) {
            $query += " AND brand_name ='$brand'";
    }
    if (!empty($cat)) {
            $query += " AND ptype = '$cat'";
    }
    if (!empty($size)) {
            $query += " AND psize= '$size'";
    }
    if (!empty($price)) {
            $query += " AND sprice = '$price'";
    }
    if (!empty($inv)) {
            $query += " AND inventory='$inv'";
    }

    if($query != ""){
         $sql = "SELECT * FROM products WHERE 1=1" . $query;
    }else{
      }

答案 1 :(得分:2)

尝试以下代码

<?php

$sql = "SELECT * FROM products WHERE 1=1 AND ";

foreach ($_REQUEST as $key => $value) {
    $columnName = '';
    switch ($key) {
        case 'name':
            $columnName = 'pname';
            break;

        case 'brand':
            $columnName = 'brand_name';
            break;

        case 'cat':
            $columnName = 'ptype';
            break;

        case 'cat':
            $columnName = 'psize';
            break;

        case 'size':
            $columnName = 'ptype';
            break;

        case 'inv':
            $columnName = 'inventory';
            break;
    }

    if (!empty($columnName) && !empty($value)) {
        $sql .= " $columnName='$value' AND";
    }
}

$sql = rtrim($sql, 'AND');

$result = $conn->query($sql);
if($result->num_rows>0){
    while($row=$result->fetch_assoc()){
        $pid = $row['pid'];
        $pname = $row['pname'];
        $pbrand = $row['brand_name'];
        $pcat = $row['ptype'];
        $pinv = $row['inventory'];
        $pprice = $row['sprice'];
        $psize = $row['psize']; ?>
        <tr id="<?php echo $pid; ?>" class="prod-details"><?php echo "<td>".$pid."</td><td>".$pname."</td><td>".$pbrand."</td>"."<td>".$pcat."</td>"."<td>".$psize."</td>"."<td>".$pprice."</td>"."<td>".$pinv."</td>"; ?></tr> <?php
    }
} 

如果我错了,请纠正我。