I cannot figure this out, I've been reading numerous Stack overflow threads and I keep getting an issue,
right now if the user types anything, regardless if it is the same username or email, it would come up with an error. Please help!
I want the error message to appear when the user types an username or email that is already on the database.
$sql = "SELECT count(*) FROM users WHERE user_email = :email OR user_name = :username";
$query1 = $DBH->prepare($sql);
$query1->execute(array(':username' => $username,':email' => $email));
if ($query1->rowCount() > 0){
// User Exists
$error = "Username or E-Mail in Use";
} else{
// User Does Not Exist
//No errors - let’s create the account
//Encrypt the password with a salt
$encryptedPass = password_hash($_POST['password'], PASSWORD_DEFAULT);
//Insert DB
$query = "INSERT INTO users (user_email, user_password, user_forename, user_lastname, user_name, user_gender, user_country, user_number) VALUES (:email, :password, :firstname, :lastname, :username, :gender, :country, :mobile)";
$result = $DBH->prepare($query);
$result->bindParam(':username', $_POST['username']);
$result->bindParam(':firstname', $_POST['firstname']);
$result->bindParam(':lastname', $_POST['lastname']);
$result->bindParam(':email', $_POST['email']);
$result->bindParam(':gender', $_POST['gender']);
$result->bindParam(':country', $_POST['country']);
$result->bindParam(':mobile', $_POST['mobile']);
$result->bindParam(':password', $encryptedPass);
if($result->execute()){
echo '<div class="alert alert-success" role="alert">Registration Successful!</div>';
echo "<script> window.location.assign('index.php?p=login'); </script>";
}
}
EDIT
$stmt = $DBH->prepare("SELECT COUNT(*) FROM users WHERE user_email = :email OR user_name = :username");
$stmt->execute(array(':username' => $username,':email' => $email));
$count = $stmt->fetchColumn();
var_dump($count);
if ($count > 0) {
$error1 = "Username or E-Mail in Use. Please try another.";
}
if(!$error){
//No errors - let’s create the account
//Encrypt the password with a salt
$encryptedPass = password_hash($_POST['password'], PASSWORD_DEFAULT);
//Insert DB
$query = "INSERT INTO users (user_email, user_password, user_forename, user_lastname, user_name, user_gender, user_country, user_number) VALUES (:email, :password, :firstname, :lastname, :username, :gender, :country, :mobile)";
$result = $DBH->prepare($query);
$result->bindParam(':username', $_POST['username']);
$result->bindParam(':firstname', $_POST['firstname']);
$result->bindParam(':lastname', $_POST['lastname']);
$result->bindParam(':email', $_POST['email']);
$result->bindParam(':gender', $_POST['gender']);
$result->bindParam(':country', $_POST['country']);
$result->bindParam(':mobile', $_POST['mobile']);
$result->bindParam(':password', $encryptedPass);
if($result->execute()){
echo '<div class="alert alert-success" role="alert">Registration Successful!</div>';
// echo "<script> window.location.assign('index.php?p=login'); </script>";
}
}
var dump = string(1) "0"