php 过滤 mysql 多个复选框

php filtering mysql multiple checkbox

我们有一个网站,公司(用户)上传他们的产品,他们插入品牌、颜色等规格,material。
当访问者想在视觉上找到产品时,他们可以选择公司(用户)之前插入的复选框,如品牌、颜色、material.

复选框的所有结果显示 Ajax

function getEmployeeFilterOptions() {
    var opts = [];
    $checkboxes.each(function () {
        if (this.checked) {
            opts.push(this.value);
        }
    });
    return opts;
}

function updateEmployees(opts) {
    $.ajax({
        type: "POST",
        url: "ajax/filterProducts.php",
        data: {
            filterOpts: opts, catIDp2:<?php echo $_GET['catIDp']; ?>
        },
        success: function (records) {
            $('#response').html(records);
        }
    })
    ;
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function () {
    var opts = getEmployeeFilterOptions();
    updateEmployees(opts);
});

现在我们这里有一个问题,复选框工作不完整,例如我们有 3 个移动复选框:
1.brands:三星、LG、苹果
2.color:红、蓝、绿
3.material:塑料、金属、玻璃

如果访客点击 Samsung(Brand) 和 Red(Color) 的复选框,则必须显示红色 Samsungs 的结果。

但我们的主要问题是访问者必须单击所有复选框(红色、金属、三星)才能显示结果。

所以我们只想显示访问者点击它的结果,而不是所有复选框都已填写。

我们的 php 代码在这里:

function getFilterProducts($cat, $filter = null){
global $db;
$result = '';
$sql = '';
if (count($filter) == 1) {
    if ($filter != null) {
        $val = implode("','", $filter);
        $sql = "SELECT productID_C,subCatID_Product,title_Product_C,status,url_seo_product,priceStatus_C,priceProduct_C,backgroundUrlPrd_C,create_date_product,priceProduct_C,countryProductFarsi_C,brandNameFarsi_Product_C,companyID,genreProductFarsi_C,colorProductFarsi_C
                FROM $db->products_company WHERE (countryProductFarsi_C IN ('$val') AND subCatID_Product='$cat') OR (brandNameFarsi_Product_C IN ('$val') AND subCatID_Product='$cat') OR (companyID IN ('$val') AND subCatID_Product='$cat') OR (genreProductFarsi_C IN ('$val') AND subCatID_Product='$cat') OR (colorProductFarsi_C IN ('$val') AND subCatID_Product='$cat') ORDER BY create_date_product DESC";
        $result = $db->query($sql);
    }
} elseif (count($filter) >= 2) {
    if ($filter != null) {
        $val = implode("','", $filter);
        $sql = "SELECT productID_C,subCatID_Product,title_Product_C,status,url_seo_product,priceStatus_C,priceProduct_C,backgroundUrlPrd_C,create_date_product,priceProduct_C,countryProductFarsi_C,brandNameFarsi_Product_C,companyID,genreProductFarsi_C,colorProductFarsi_C
                FROM $db->products_company WHERE subCatID_Product='$cat' AND countryProductFarsi_C IN ('$val') AND brandNameFarsi_Product_C IN ('$val') AND companyID IN ('$val') AND genreProductFarsi_C IN ('$val') AND (colorProductFarsi_C IN ('$val')) ORDER BY create_date_product DESC";
        $result = $db->query($sql);
    }
} else {
    $sql = "SELECT productID_C,subCatID_Product,title_Product_C,status,url_seo_product,priceStatus_C,priceProduct_C,backgroundUrlPrd_C,create_date_product,priceProduct_C,countryProductFarsi_C
            FROM $db->products_company WHERE subCatID_Product='$cat' ORDER BY create_date_product DESC";
    $result = $db->query($sql);
}
if ($result) {
    $listFilterPrd = $result->fetch_all(MYSQLI_ASSOC);
    return $listFilterPrd;
}
return null;}

最后我们希望复选框像 amazon.com 或 ebay.com

一样正常工作

我们如何解决这个问题?

This image is our product check boxes

我认为你应该动态地准备查询 例如

谨防SQL注射

注意:我只是在写伪代码(只是关于如何实现的想法)请根据需要进行调整

$sql ="select * from products where 1";
if($filter['color'])
   $sql.="and color='$color'";
if($filter['manufracture'])
   $sql.="and color='$manufracture'";
....
and lastly execute the query.