Reputation: 191
I have a class that inserts the user from a registration form into a database. I am inserting 4 things: username, password, salt and an email. I am using errno 1062 to check for duplicate entries. But I need to check for specific duplicate entries. For example I have this logic that checks for errno 1062 but it's really not specific to the username being checked, it's just checking for an errno and assuming it's the username that's duplicated.
if ($stmt->affected_rows == 1) {
$this->success = "$this->username has registered. You can now log in.";
} elseif ($stmt->errno == 1062) {
$this->errors[] = "$this->username is already in use. Please chose another.";
} else {
$this->errors[] = 'Sorry, there was an issue with the database.';
}
My question is, how could I use errno 1062 to check for both username, email or both? Seems like errno 1062 just checks for ANY duplicate entries.
Upvotes: 0
Views: 1125
Reputation: 74
errno only returns an integer, with no other information about what error occurred, but you should be able to use error for more information.
According to the manual, if you have an errno 1062, error should be a string in this format:
Duplicate entry '%s' for key '%s'
You can parse that string to get the data you want using normal PHP string functions. If you have more than one error it will only 'complain' about the first one it finds (at least on my servers), so be aware you might end up asking the user to fix one thing, then coming back to correct another, etc. The only way I know of to improve that would be to run separate simple queries:
SELECT COUNT(*) FROM table WHERE email = '[email protected]'
P.S. Do you really want to block duplicate passwords?
Upvotes: 1