Issue with PDO check


#1

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>";
		}
	}
}
?>

#2

Welcome to the forum.

I am not sure but maybe there are spaces in the userName and requires wrapping userName in quotations.

PDO has error checking which may help determine the problem.

This comprehensive PDO tutorial may also help:


#3

Thank you for your reply. I haven't figured it out yet, as I don't really see a lot of documentation on it, but I'll figure it out. I always do lol. Thank you for the links man. Appreciate it.

Be blessed.


#4

why the brackets around $email?

EDIT:
Don't think I've ever passed a value that way.


#5

I'm still learning PDO, so it's quite new to me. The brackets work just fine when checkin them email in the database, so I figured it would work forth e username as well. Kinda strange it doesn't.


#6

here's the ref for the use of brackets with the PDO execute() method:
http://php.net/manual/en/pdostatement.execute.php

Pls check what's the $username array.


#7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.