Handle error for duplicate entries - PHP MySQL

后端 未结 5 1716
礼貌的吻别
礼貌的吻别 2020-11-28 09:48

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,

5条回答
  •  难免孤独
    2020-11-28 10:45

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

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

提交回复
热议问题