function checkDuplicateName($table, $column_name, $value, $db){
try{
$sqlQuery = "SELECT * FROM ".$table." WHERE ".$column_name." = :$column_name";
$statement = $db->prepare($sqlQuery);
$statement->execute(array(':column_name' => $value));
if($row = $statement->fetch()){
return true;
}
return false;
} catch(PDOException $e){
}
}
if(checkDuplicateName('users', 'email', $username, $db)){ //$db is the database connection name
$result = flashMessage("Username is already taken");
}
else if(checkDuplicateName('users', 'email', $email, $db)){
$result = flashMessage("Email is already taken");
}
This function checks if the username and email is already taken or not. The problem is when I try to submit the form, it shows me this error instead of pre-defined flashmessage() function.
An error occured: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘demo@yahoo.com’ for key ‘email’