mySQL和PHP从数据库中过滤$ _GET结果

时间:2016-05-31 02:23:46

标签: php mysql get

我的代码从html表单接收品牌,然后继续从数据库生成搜索结果。

      if(isset($_GET['brand'])) {
    $brand = mysqli_real_escape_string($db, $_GET['brand']);
    echo $brand;

    $featuredSql = "SELECT t.*, s.quantity, s.rrp AS nRetailPrice, r.city AS city_name, ts.thumbnail as tread_thumbnail, ts.bigpic as tread_bigpic, t.rating FROM tyres t INNER JOIN stocklevels s 
ON t.stockcode=s.stockcode LEFT JOIN tyre_treads ts ON t.treadid=ts.recid LEFT JOIN reseller r ON s.city=r.recid WHERE s.quantity>0 ";


    if ($brand != 'Any') $featuredSql .= " AND t.manufacturer='$brand'";
    $featuredSql .= "GROUP BY t.diam,t.manufacturer, t.width";
    if (!$featured = mysqli_query($db, $featuredSql)) die(mysqli_error());
    var_dump($featured);
    if (!$tyre1 = mysqli_fetch_object($featured)) echo "<br />No tyres match those parameters. Please try again.";
}

用于表示曾经收集的数据的HTML。

 <div class="text text-background">
 <h4>
     <a href="detail.php"><?= $product["profile"] . "/" . $product["width"] . "/" . $product["diam"] . " " . $product['manufacturer']; ?></a>
 </h4>
     <p class="brand"><a href="#"><?= $product["part_no"]; ?></a></p>
     <div class="col-md-12 col-sm-12 col-xs-12 text-right">
        <div class="txtprice-small">PRICE FROM $<span><?= $product["nRetailPrice"]; ?></span></div>
     </div>
     <table class="table table-condensed small">
        <tbody>
           <tr>
              <td width="40%">SIZE:</td>
              <td width="60%"><?= $product["diam"]; ?></td>
           </tr>
           <tr>
              <td>Width:</td>
              <td><?= $product["width"]; ?></td>
           </tr>
           <tr>
              <td>Profile:</td>
              <td><?= $product["profile"]; ?></td>
           </tr>
           <tr>
              <td>Stock:</td>
              <td><?= $product["quantity"]; ?></td>
           </tr>
           <tr>
              <td>Rating:</td>
              <td><?= $product["rating"] ?></td>
           </tr>
        </tbody>
    </table>


        if ($brand != 'Any') $featuredSql .= " AND t.manufacturer='$brand'";
        $featuredSql .= "GROUP BY t.diam,t.manufacturer, t.width";
        if (!$featured = mysqli_query($db, $featuredSql)) die(mysqli_error());
        var_dump($featured);
        if (!$tyre1 = mysqli_fetch_object($featured)) echo "<br />No tyres match those parameters. Please try again.";
    }

现在问题是数据库维护不善而且有几个项目是重复的(因此GROUP BY直径,制造商和宽度),但每个项目都有自己的库存,例如

商品part_no abc stock 5, 项目part_no abc stock 7

所以实际上有12个,但都被列为单独的项目,GROUP_BY将它们组合在一起,但对库存水平没有任何作用。

我的问题是我该如何处理?我已经看过尝试在原始Query中使用COUNT,但是如果我这样做的话,带有fetch assoc的while循环会中断。虽然我可以错误地编码它(相当新的PHP和使用MySQL)。 我也可以GROUP BY part_no但这似乎也没有帮助。

推荐的其他信息

轮胎的数据库 RecID是唯一主键 part_no不是唯一的 stockCode IS UNIQUE

样本数据

    \/65\/14-4015","thumbnail":null,"bigpic":null,"rating":"82H","tread":null,"profile":"65","width":"175","diam":"14","description":null,"rrp":"0.00","flag":null,"notes":"","treadid":"707","hide":"no","quantity":"1","nRetailPrice":"79.00","city_name":"New Plymouth","tread_thumbnail":"707_thumb.jpg","tread_bigpic":"707_large.jpg"},

{"recid":"3705","part_no":"ALT185\/65\/14","manufacturer":"ALTENZO","supplier":null,"stockcode":"TY-ALT185\/65\/14-3705","thumbnail":null,"bigpic":null,"rating":"86H","tread":null,"profile":"65","width":"185","diam":"14","description":null,"rrp":"0.00","flag":null,"notes":"","treadid":"707","hide":"no","quantity":"1","nRetailPrice":"79.00","city_name":"Palmerston North","tread_thumbnail":"707_thumb.jpg","tread_bigpic":"707_large.jpg"},

{"recid":"4557","part_no":"ALT185\/60\/15","manufacturer":"Altenzo","supplier":null,"stockcode":"TY-ALT185\/60\/15-4557","thumbnail":null,"bigpic":null,"rating":"88H","tread":null,"profile":"60","width":"185","diam":"15","description":null,"rrp":"0.00","flag":null,"notes":"","treadid":"707","hide":"no","quantity":"2","nRetailPrice":"99.00","city_name":"Petone","tread_thumbnail":"707_thumb.jpg","tread_bigpic":"707_large.jpg"},

{"recid":"2827","part_no":"ALT195\/50\/15","manufacturer":"ALTENZO","supplier":null,"stockcode":"TY-ALT195

0 个答案:

没有答案