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,
Use mysql_errno() function, it returns the error numbers. The error number for duplicate keys is 1062. for example
mysql_errno()
$query = mysql_query("INSERT INTO table_name SET ...); if (mysql_errno() == 1062){ echo 'Duplicate key'; }