mysql重复错误处理

mysql Duplicate error handling

我正在尝试使用 PHP 从表单中输入数据。当我尝试输入重复数据时,会弹出一条错误消息,例如

这里出了点问题:

INSERT INTO customer VALUES('jamie9422','Jamie Lannister','sept of baelor','jamie@cersei.com',9422222222,0) Duplicate entry 'jamie9422' for key 'PRIMARY' "

相反,我想显示一条干净的错误消息。我怎样才能做到这一点。这是我到目前为止编写的代码...

<?php
     include_once "dbConnect.php";
     $connection=connectDB();

    if(!$connection)
    {
        die("Couldn't connect to the database");
    }
    $tempEmail = strpos("{$_POST["email"]}","@");

    $customer_id=substr("{$_POST["email"]}",0,$tempEmail).substr("{$_POST["phone"]}",0,4);

    //$result=mysqli_query($connection,"select customer_id from customer where customer_id='$customer_id' ");
    //echo "customer_id is".$result;

    $query = "SELECT * FROM CUSTOMER WHERE CUSTOMER_ID='$customer_id'";

    $customer_idcip = $customer_id-1;
    echo $customer_idcip;

    if ( mysql_query($query)) {
        echo "It seems that user is already registered";
    } else {
        $command = "INSERT INTO customer VALUES('{$customer_id}','{$_POST["name"]}','{$_POST["address"]}','{$_POST["email"]}',{$_POST["phone"]},0)";

        $res =$connection->query($command);
        if(!$res){
            die("<br>Something went wrong with this:{$command}\n{$connection->error}");
        }

        echo "Welcome ".$_POST["name"]." \nCongratulations on successful Registration. Refill your Wallet here";

        //$cutomerRetrival = mysql_query("select  from customer where customer_id='$customer_id'");
        echo "<br>Please note your customer ID :".$customer_id;                 
    }       
    /*if($result)
    {
        echo "Query Fired";
        $dupentry = mysqli_num_rows($result);
        if($dupentry==1)
        {
            echo "You are already Registered";
            exit;
        }
    }*/
?>

error code (number) is 1022.
你可以例如为此定义一个常量(以便 x 个月后的其他人有机会理解代码),如

define('ER_DUP_KEY', 1022);

然后做类似

的事情
if(!$res){
  if ( <error code>==ER_DUP_KEY ) {
    handleDuplicateEntryError();
  }
  else {
    die("<br>Something went wrong with this:{$command}\n{$connection->error}");
  }
}

因为我不知道 $res =$connection->query($command); 是如何工作的(以及 $connection 是什么我不能告诉你具体如何实现 <error code>==ER_DUP_KEY,可以使用 mysql_errno. 但它似乎以某种方式与 mysql_query($query) 混合在一起,即旧的、已弃用的 mysql_* 扩展和一些自定义 class。您可能想先解决这个问题……;-)
http://docs.php.net/manual/en/mysqlinfo.api.choosing.php

改用Try Catch

try{
   $res =$connection->query($command);
}catch(Exception $e){
  die( "Write your error appropriate message here"); 
}

您的代码未正确检查现有记录

改变

if (mysql_query($query)) {
    echo "It seems that user is already registered";
} 

$result = mysql_query($query);
if (mysql_num_rows($result)) {
    echo "It seems that user is already registered";
} 

另外,请不要在没有转义的情况下使用 $_POST 变量,使用 mysql_real_escape_string() 之类的东西来转义从用户传递的每个变量,否则你的网站将很快被 SQL 注入.

对您的文件进行一些更新,然后尝试获取错误消息 'customer already registered.'

$query = "SELECT * FROM CUSTOMER WHERE CUSTOMER_ID='$customer_id'";
$res= mysql_query($query);
$customer_count = mysql_num_rows($res);

$customer_idcip = $customer_id-1;
echo $customer_idcip;

if ( $customer_count > 0 ) {
echo "It seems that user is already registered";
} else {
  ...................................

谢谢大家。 实际上我在 connectDB.php 文件中使用 mysqli API.. 因此我需要在 mysqli 上调用函数。 相反,我打电话给 mysql。即我正在创建一个新连接,因此根本没有触发查询。 更改为面向对象样式的 mysqli->query($result) 而且效果很好....