处理重复条目的错误 - PHP MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3146838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:50:47  来源:igfitidea点击:

Handle error for duplicate entries - PHP MySQL

phpmysqlerror-handling

提问by RobHardgood

I have a PHP form which enters data into my MySQL database. My primary key is one of the user-entered values. When the user enters a value that already exists in the table, the MySQL error "Duplicate entry 'entered value' for key 1" is returned. Instead of that error, I would like to alert the user that they need to enter a different value. Just an echoed message or something. I guess my question comes down to: how to turn a specific MySQL error into a PHP message Thanks

我有一个 PHP 表单,可以将数据输入到我的 MySQL 数据库中。我的主键是用户输入的值之一。当用户输入表中已存在的值时,返回 MySQL 错误“Duplicate entry 'entered value' for key 1”。而不是那个错误,我想提醒用户他们需要输入不同的值。只是一个回显的消息或其他东西。我想我的问题归结为:如何将特定的 MySQL 错误转换为 PHP 消息 谢谢

edit: nickf's answer below is nice, but is there any way to discern between specific errors?

编辑:nickf 在下面的回答很好,但是有什么方法可以区分特定错误吗?

回答by jensgram

To check for this specific error, you need to find the error code. It is 1062for duplicate key. Then use the result from errno()to compare with:

要检查此特定错误,您需要找到错误代码。它1062用于重复密钥。然后使用结果与以下内容errno()进行比较:

mysqli_query('INSERT INTO ...');
if (mysqli_errno() == 1062) {
    print 'no way!';
}

A note on programming style
You should always seek to avoid the use of magic numbers(I know, I was the one to introduce it in this answer). Instead, you could assign the known error code (1062) to a constant (e.g. MYSQLI_CODE_DUPLICATE_KEY). This will make your code easier to maintain as the condition in the ifstatement is still readable in a few months when the meaning of 1062has faded from memory :)

关于编程风格的说明
您应该始终避免使用幻数(我知道,我是在这个答案中介绍它的人)。相反,您可以将已知错误代码 ( 1062)分配给一个常量 (例如MYSQLI_CODE_DUPLICATE_KEY)。这将使您的代码更易于维护,因为if语句中的条件在几个月后仍然可读,而 的含义1062已从记忆中消失:)

回答by nickf

You can check the return value from mysql_querywhen you do the insert.

您可以检查mysql_query插入时的返回值。

$result = mysql_query("INSERT INTO mytable VALUES ('dupe')");

if (!$result) {
    echo "Enter a different value";
} else {
    echo "Save successful.";
}

回答by Hassan Saeed

try this code to handle duplicate entries and show echo message:

尝试使用此代码处理重复条目并显示回显消息:

  $query = "INSERT INTO ".$table_name." ".$insertdata;
                if(mysqli_query($conn,$query)){
                    echo "data inserted into DB<br>";                   
                }else{
                   if(mysqli_errno($conn) == 1062)
                       echo "duplicate entry no need to insert into DB<br>";
                   else
                    echo "db insertion error:".$query."<br>";

                }//else end

回答by Codler

回答by Chad timothy

Use mysql_errno()function, it returns the error numbers. The error number for duplicate keys is 1062. for example

使用mysql_errno()函数,它返回错误号。重复键的错误号是 1062。例如

$query = mysql_query("INSERT INTO table_name SET ...);
if (mysql_errno() == 1062){
    echo 'Duplicate key';
}