I have a PHP code and a field in the database which is a unique field. If people fill in the form and if the $_POST['name'] is already in the database it gives an error.
That’s what I have and want, but now I want to check if there’s an error so I can handle it in a if / else statement.
This is my code:
$db = new database();
$sql = "INSERT INTO product_groepen (name) VALUES (".$_POST['name'].")";
$result = $db->executeQuery($sql);
if ($result)
{
$db->executeQuery($sql);
$page .= 'Yes';
} else {
$page .= 'No';
}
The error:
Warning: PDO::query() [pdo.query]: SQLSTATE[23000]: Integrity
constraint violation: 1062 Duplicate entry ‘s’ for key 2 in
/classes/database.class.php on line 26
It works, and when it isn’t working it says ‘no’, but the error remains.
try with
INSERT IGNOREto ignore insert if it’s duplicate. Also if you are still usingmysql_*you have anmysqlinjection vulnerability, escape it:and make sure you don’t execute the query twice