php - 如何在 MySQL 中重复插入行时抛出错误

标签 php mysql error-handling insert

我有两个字段 Aadhaar numberPAN Number 字段 UNIQUE 到数据库中

我的要求是,每当我插入一些重复的条目时,它只会给我一个错误

Could Not Enter, Duplicate Aadhaar and/or PAN Number

MySQL 有什么方法可以抛出这种错误吗?

最佳答案

您需要找到错误代码。重复键为 1062。您可以像下面的代码一样处理它。

mysqli_query($con,'INSERT INTO ...');
if (mysqli_errno($con) == 1062) {
    print 'Could Not Enter, Duplicate Aadhaar and/or PAN Number';
}

use mysqli_errno() to retrieve the error code. Note that this function only returns the error code from the most recently executed MySQL function.

关于php - 如何在 MySQL 中重复插入行时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51322936/

相关文章:

php - zend框架1 zend_db在不同条件下结果相同

php - Cookie 多个值

php - PHP结束标记:=?>

mysql选择两列之和小于一列的记录

go - 为什么接受自定义结构来代替错误?

php - "XML Parsing Error: junk after document element"

python - 循环将数据从串口读取到数据库

php - PHP MySQL SELECT 中的嵌套 if 语句

error-handling - Go 中如何指定和处理特定的错误?

php - 如何集中处理php错误和异常