小编典典

插入前检查重复项

mysql

在插入数据库之前,我正在使用以下代码检查重复项。对我来说,重复的只考虑重复时namedescriptionpricecity,和enddate比赛。

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。


阅读 270

收藏
2020-05-17

共1个答案

小编典典

您要执行以下操作:

$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 ...
}

有关更多信息,请参见此处

2020-05-17