I have a table with two Unique fields: username & email. My registration form prevents a user from registering an already-registered username and email address. My problem is I don't know how to distinguish which threw the error when the Try/Catch block catches this. Using this:
catch (PDOException $ex){
if ($ex->getCode() == 23000){ //Check if it's a duplciate key violation.
$unameErr = '* Username already in use: '.$ex;
}
I get this:
* Username already in use: exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cowboy@dfwit.co' for key 'email'' in /var/www/html/dfwit/register.php:36 Stack trace: #0 /var/www/html/dfwit/register.php(36): PDOStatement->execute(Array) #1 {main}
That tells me the 'email' key violated and threw the error. But I have no clue how to use that in the Try/Catch block to differentiate between the two possible keys. This is my latest try:
catch (PDOException $ex){
if (($ex->getCode() == 23000) && (key(array &$username))) { //Check if it's a duplciate key violation.
$unameErr = '* Username already in use: '.$ex;
} elseif (($ex->getcode() == 23000) && (key(array &$email))) {
$emailErr = '* Email address already in use: '.$ex;
}
but that leaves me with a syntax error. I've smoked down the Google servers on this one with nothing to show for it. If you know how to code this, I'd appreciate your insight. Many thanks.