php – 插入前检查重复项

前端之家收集整理的这篇文章主要介绍了php – 插入前检查重复项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在插入数据库之前,我使用以下代码来检查重复项.对我来说,当名称,描述,价格,城市和结束日期匹配时,副本仅被视为重复.
foreach($states_to_add as $item) {
    $dupesql = "SELECT 
                    COUNT(*) 
                FROM 
                    table 
                WHERE 
                    (   
                        name = '$name' 
                        AND description = '$description' 
                        AND manufacturer = '$manufacturer' 
                        AND city ='$city' 
                        AND price = '$price' 
                        AND enddate = '$end_date'
                    )";

    $duperaw = MysqL_query($dupesql);

    if($duperaw > 0) {
        echo nl2br("$name already exists in $city \n");
    } 
    else {
        $sql = "INSERT INTO table (..... (here go the values to be inserted)
        ....

在运行此检查之前定义每个值,我的结果总是在项目已存在时返回.我倾倒了“dupesql”并将命令复制/粘贴到PHPmyadmin中,后者返回计数0.

您想要执行以下操作:
$dupesql = "SELECT * FROM table where (name = '$name' AND description = '$description' AND manufacturer = '$manufacturer' AND city ='$city' AND price = '$price' AND enddate = '$end_date')";

$duperaw = MysqL_query($dupesql);

if (MysqL_num_rows($duperaw) > 0) {
  //your code ...
}

有关更多信息,请参见Here.

原文链接:/php/135594.html

猜你在找的PHP相关文章