// Insert User, if all other tests are happy
if( ( $_POST['name'] != "" && $_POST['email'] != "" ) && ( $errors['count'] == 0 ) ) {
try {
$insertUser = $DB->prepare("
INSERT INTO
users (
u_user, u_email, u_password, u_added
)
VALUES (
:user, :email, :password, NOW()
)
");
$insertUser->execute( [
':user' => $_POST['email'],
':email' => $_POST['email'],
':password' => password_hash(randomPassword(), PASSWORD_DEFAULT)
] );
}
catch (PDOException $e) {
if ($e->getCode() == "23000" ) {
$errors['count']++; $errors['desc'] = [ "user_duplicate_record" ];
}
}
}
Can I wrap this test into a database wrapper - as having to duplicate this code on every possible scenario where this may happen seems cumbersome? or this a better way to do this kind of thing?