如何检查mysql数据库中的重复记录

时间:2015-12-08 09:55:07

标签: php mysql

$queryb = "SELECT product, imei, country, warranty, config from PRODUCT WHERE product_slno = '$mnserialno' ";
$resultb = mysql_query($queryb, $gndbconn) ;
if(mysql_num_rows($resultb) > 0)
{
    $queryc = "UPDATE PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config' WHERE product_slno = '.$mnserialno.' ";
     $resutc = mysql_query($queryc, $gndbconn) ;
 }
else{
    $querya  = "INSERT INTO PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config'";
    $resulta = mysql_query($querya, $gndbconn) ; 
 }  

我想查看序列号,如果该序列号已存在于数据库中,则记录会更新,否则会插入到数据库中。 但是只插入记录的代码,没有更新,我没有得到什么错, 如何防止重复输入?

1 个答案:

答案 0 :(得分:1)

INSERT INTO PRODUCT SET
    (`product`, `product_slno`, `imei`, `country`, `warranty`, `config`)
VALUES
    ('" . $desc . "',  '" . $mnserialno . "', '" . $imei . "', '" . $country . "', '" . $warranty . "', '" . $config . "')
ON DUPLICATE KEY UPDATE
    product='" . $desc . "',
    product_slno='" . $mnserialno . "',
    imei='" . $imei . "',
    country='" . $country . "',
    warranty='" . $warranty . "',
    config='" . $config . "'";