Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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 - Fatal编程技术网

Php 如何找出数据何时匹配并显示消息

Php 如何找出数据何时匹配并显示消息,php,Php,当客户和产品名称匹配时,显示是,如果不是,显示否 shopping cart table --------------------------- id|product name | customer ----------------------------- 1 |book | Tomic 2 |pencil | Tomic 3 |shoe | Jasper ------------------------------- pr

当客户和产品名称匹配时,显示是,如果不是,显示否

shopping cart table
---------------------------
id|product name   | customer
-----------------------------
1 |book           |  Tomic
2 |pencil         |  Tomic
3 |shoe           |  Jasper
-------------------------------


product name| 
--------------------------
book        | add to cart
---------------------------
当Tomiccustomer单击添加到购物车显示是时,如果Jaspercustomer单击添加到购物车显示否

if(isset($_POST['submit1']))
{
    $customer = $_SESSION['login_user'];

    $res2 = mysqli_query($mysqli,"SELECT * FROM shopping_cart where product_name='$product_name'");

    while($row2=mysqli_fetch_array($res2))
    {
        if($customer==$row2['customer'])
        {
            echo "yes";
        }
        else
        {
            echo "no";
        }
    }
}
将查询更改为“是”或“否”条件

"SELECT * FROM shopping_cart where product_name='$product_name' and customer='$customer'"
您可以使用mysqli_num_行


问题是什么?如果$customer=='Tomic'{…?我得到的结果是Yes你得到的是Yes,因为购物车中有两个商品具有匹配的产品名称将用户与post$user=$\u post['user']并更改查询中的WHERE子句:WHERE product_name='$product_name'和customer=$user。如果没有返回结果,则为否,否则为是,但当然,不要像那样使用$customer以避免SQL注入。
$data=mysqli_num_rows($res2);
if($data>0){
    echo "yes";
    }
    else{
    echo "no";
    }