Page 1 of 1
Duplicate Key Error
Posted: Mon May 29, 2006 2:01 am
by tecktalkcm0391
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
Posted: Mon May 29, 2006 2:52 am
by harrisonad
simple suggestion:
- I suggest that you must first disable the error reporting on php.ini
- Trap the mysql error and Identify it
- If it matches the DUPLICATE error, spit your customzed message
Posted: Mon May 29, 2006 3:54 am
by rllqph
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.
Posted: Mon May 29, 2006 4:12 am
by timvw
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.
Posted: Mon May 29, 2006 7:51 am
by tecktalkcm0391
Can anyone tell me how do to what rllqph was taking about?
Posted: Mon May 29, 2006 10:26 am
by Ambush Commander
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
}
Posted: Mon May 29, 2006 10:45 am
by tecktalkcm0391
thank you
Posted: Wed Jul 19, 2006 6:39 pm
by tecktalkcm0391
finally used it and for anyone else it is 1062 not 23000