Can you make it so if a MySQL database returns a duplicate key error, it displays "Sorry but this e-mail has already been used" instead of "Duplicate entry 'example@example.com' for key 2"
Thanks
Duplicate Key Error
Moderator: General Moderators
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
what i normally do is this...
- perform a query that look for matched entry in the db and user input.
e.g. SELECT email FROM account WHERE email = $inputemail
- if there's a result, then spit out the customize error message.
- if no result, then it means that the email address is not present in the db.
- perform a query that look for matched entry in the db and user input.
e.g. SELECT email FROM account WHERE email = $inputemail
- if there's a result, then spit out the customize error message.
- if no result, then it means that the email address is not present in the db.
The problem with 'verifying if there is already such an id' is that between the verification and the insertion there is a gap (doesn't matter how big it is, it is there). This gap can lead to problems (eg: another user is registered between the verification and insertion) so you would need to use at least transactions to make these two actions look like one, atomic, action.
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Here's how I would do it.
Code: Select all
$result = @mysql_query($query_that_might_have_unique_key_violation);
if (mysql_errno() == 23000) { //that's the error number for duplicate unique (I think)
// duplicate email
}- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida