2

I have two unique keys 'phone' and 'email'. When a duplicate key is getting inserted I'm getting the error posted below:

Integrity constraint violation: 1062 Duplicate entry '4294967295-xyz@gmail.com' for key 'phone'

What I'm trying to do is display the thrown error to the user like posted below:

1. Duplicate entry '4294967295' for key 'phone'
2. Duplicate entry 'xyz@gmail.com' for key 'email'

How can I do that?

Here's what I have done to get the error displayed on front-end:

catch( PDOException $Exception ){
    if ($Exception->errorInfo[1] == 1062) {
      echo 'ERROR: ' . $Exception->getMessage();
   }
}
Amit Singh
  • 2,267
  • 4
  • 25
  • 50
  • Please show us your code and your attempts – Rizier123 Feb 28 '15 at 10:03
  • 1
    Try this: `echo $stmt->errorInfo()[2];` Is this what you want? – Rizier123 Feb 28 '15 at 10:14
  • 4
    Instead of parsing PDO exceptions, I would probably use different queries (and ajax as soon as a field looses focus...) to validate the different input fields. That would also give you a lot more flexibility where the error messages are concerned. – jeroen Feb 28 '15 at 10:14
  • You may like to take a look http://stackoverflow.com/questions/8776344/how-to-view-query-error-in-pdo-php – Vivek Vaghela Feb 28 '15 at 10:17
  • 1
    @popla BTW: I think your only able to get 1 error message, because the second one will never be found, because it stops when it finds the first error – Rizier123 Feb 28 '15 at 10:26
  • 1
    @Rizier123: Yes, you're right. Seems Like jeroen's way is the only way out. – Amit Singh Feb 28 '15 at 10:28

1 Answers1

-1

You should not be inserting a value in your auto-increment field. The system will create it for you.