I’ve been busy for hours trying to figure out why it won’t search the username; if it exists in the database or not. If I use email, it works fine for checking the email, but why not the username?
$check_email = $con->prepare("SELECT email from tbl_users WHERE email = ?");
$check_email->execute([$email]);
if($check_email->rowCount() == 1) {
$error = "<div class='error_msg'>Email already exists!</div>";
This works perfect and it does check the database for existing email.
now if I change it for the username info, it just bypasses the code in the script and acts as if it’s non existent.
Is there any reason why? This is what I put it as:
$check_user= $con->prepare("SELECT username from tbl_users WHERE username= ?");
$check_user->execute([$username]);
if($check_user->rowCount() == 1) {
$error = "<div class='error_msg'>Username already exists!</div>";
Below is the entire code without the username check. When I add it though, it doesn’t work as I said, it just bypasses it and everything else is considered, while it puts all the information into the database without checking if the username exists.
if(isset($_POST['create_account'])) {
$error = '';
$username = trim($_POST['username']);
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$confirm_pass = trim($_POST['confirm_pass']);
$country = trim($_POST['country']);
$sex = trim($_POST['gender']);
$month = trim($_POST['month']);
$day = trim($_POST['day']);
$year = trim($_POST['year']);
$dateOfBirth = trim($_POST['month']." ". $_POST['day'].", ".$_POST['year']);
$age = trim($_POST['age']);
$state = trim($_POST['state']);
$denomination = trim($_POST['denomination']);
If(empty($username) || empty($first_name) || empty($last_name) || empty($email) || empty($password) ||
empty($confirm_pass) || empty($country) || empty($sex) || empty($month) || empty($day) || empty($year) || empty($dateOfBirth) ||
empty($age) || empty($state) || empty($denomination)) {
$error = "<div class='error_msg'>Fill Out Form Completely!</div>";
} else {
$pattern = "/^[a-zA-Z ]+$/";
if(preg_match($pattern, $first_name)) {
if(preg_match($pattern, $last_name)) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
if(strlen($password )> 6 && strlen($confirm_pass) > 6) {
if($password == $confirm_pass) {
$check_email = $con->prepare("SELECT email from tbl_users WHERE email = ?");
$check_email->execute([$email]);
if($check_email->rowCount() == 1) {
$error = "<div class='error_msg'>Email already exists!</div>";
} else {
$code = rand();
$email_activated = 0;
$chat_mute = 1;
$auth_id = 1;
$timestamp = date("Y-m-d");
$insert_query = $con->prepare("INSERT INTO tbl_users(chat_mute, username, first_name, last_name, auth_id, email, password, sex, birthday, country, age, state, denomination, code, email_activated, regdate)
VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$insert_query->execute([$chat_mute, $username, $first_name, $last_name, $auth_id, $email, password_hash($password, PASSWORD_DEFAULT), $sex, $dateOfBirth, $country, $age, $state, $denomination, $code, $email_activated, $timestamp]);
}
} else {
$error = "<div class='error_msg'>Your Passwords do not match!</div>";
}
} else {
$error = "<div class='error_msg'>Your Password Is Too Weak!</div>";
}
} else {
$error = "<div class='error_msg'>Invalid Email!</div>";
}
} else {
$error = "<div class='error_msg'>Last Name Cannot Contain Numbers Or Symbols!</div>";
}
} else {
$error = "<div class='error_msg'>First Name Cannot Contain Numbers Or Symbols!</div>";
}
}
}
?>