Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:避免将同一产品显示两次?_Php_Mysql - Fatal编程技术网

PHP:避免将同一产品显示两次?

PHP:避免将同一产品显示两次?,php,mysql,Php,Mysql,我正在使用以下代码使用PHP/MYSQL为我的站点创建一个搜索函数 如果选中复选框,此代码将搜索尺寸和颜色,并在“我的页面”中显示它们 例如: checkbox 1 = small checkbox 2 = large checkbox 3 = xxlarge checkbox 4 = red checkbox 5 = black etc etc ...... 如果用户选择的大小为“小”和“大”,颜色为“红”,my PHP将返回具有这些凭据的产品 这是PHP代码: <?php error

我正在使用以下代码使用PHP/MYSQL为我的站点创建一个搜索函数

如果选中复选框,此代码将搜索尺寸和颜色,并在“我的页面”中显示它们

例如:

checkbox 1 = small
checkbox 2 = large
checkbox 3 = xxlarge
checkbox 4 = red
checkbox 5 = black
etc etc ......
如果用户选择的
大小为“小”和“大”,颜色为“红”
,my PHP将返回具有这些凭据的产品

这是PHP代码:

<?php
error_reporting(-1);
ini_set('display_errors', 'On'); 
include "config/connect.php";
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c')";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }
        //print "SQL Query: $sql<br />"; //<-- Debug SQl syntax.
        // Run query outside of foreach loop so it only runs one time.
        $query = mysqli_query($db_conx, $sql);
        //var_dump($query); //<-- Debug query results.
        // Check that the query ran fine.
        if (!$query) {
            print "ERROR: " . mysqli_error($db_conx);
        } else {
            // Use $query inside this section to make sure $query exists.
            $productCount = mysqli_num_rows($query);
            $i=0; // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                   $id = $row["id"];
                   $product_name = $row["product_name"];
                   $searchList .= ''.$product_name.'';
                }
            }
        }
    }
}
?> 
但这会引发以下错误:

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id WHERE (ATTRIBUTES.sizes LIKE BIN' at line 3
您需要使用SQL“分组依据”函数。如果yt.id是您的产品id,那么将是附加到查询中的“groupbyyt.id”

尝试:



尝试
选择不同的*…
@ᵈˑ,不,那不行。它仍然返回重复的项。因为“项目”表与“颜色和大小”表是分开的。对不起,我应该在我的问题中提到这一点。同样的错误。事实上,在我尝试另一种方法之前,我先像你一样尝试了。两者都抛出相同的错误。
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id WHERE (ATTRIBUTES.sizes LIKE BIN' at line 3
<?php

error_reporting(-1);
ini_set('display_errors', 'On');
include "config/connect.php";
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c')";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }

        //append "GROUP BY"
        $sql .= " GROUP BY yt.id ";

        //print "SQL Query: $sql<br />"; //<-- Debug SQl syntax.
        // Run query outside of foreach loop so it only runs one time.
        $query = mysqli_query($db_conx, $sql);
        //var_dump($query); //<-- Debug query results.
        // Check that the query ran fine.
        if (!$query) {
            print "ERROR: " . mysqli_error($db_conx);
        } else {
            // Use $query inside this section to make sure $query exists.
            $productCount = mysqli_num_rows($query);
            $i=0; // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                    $id = $row["id"];
                    $product_name = $row["product_name"];
                    $searchList .= ''.$product_name.'';
                }
            }
        }
    }
}
?>