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

时间:2018-03-17 09:17:37

标签: php

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

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


product name| 
--------------------------
book        | add to cart
---------------------------

当Tomic(客户)点击添加到购物车显示“是”时,如果Jasper(客户)点击添加到购物车显示“否”

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";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

更改yesno条件

的查询
"SELECT * FROM shopping_cart where product_name='$product_name' and customer='$customer'"

您可以使用mysqli_num_rows();

$data=mysqli_num_rows($res2);
if($data>0){
    echo "yes";
    }
    else{
    echo "no";
    }