PHP register check for user in database [closed]


PHP register check for user in database [closed]



I created a function that checks for user in database but when I type the email that exists, it returns the message, and then, on second try can't proceed to register function, can't get rid of error, here is my try:


if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";
} else if($user->doesUserExist($email) > 0) {
$email_error = "Email is already taken";
} else {
$email_error = "";
}

if(empty($error) && empty($email_error) && empty($confirm_password_error)) {
$user->register($username, $name, $email, $password);
}



It's not problem with register function.



Here are the class functions:


public function doesUserExist($email) {
$query = "SELECT count(*) FROM users WHERE email = :email";
$stmt = $this->connection->prepare($query);
$stmt->bindParam(':email', $email);
$stmt->execute();
$rowcount = $stmt->rowCount();
if($rowcount > 0) {
return true;
} else {
return false;
}
}

public function register($username, $name, $email, $password) {
try {
$newPassword = password_hash($password, PASSWORD_DEFAULT);
$query = 'INSERT INTO users (username, name, email, password) VALUES (:username, :name, :email, :password)';
$stmt = $this->connection->prepare($query);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $newPassword);
$stmt->execute();

header("Location: login.php");

} catch(PDOException $e) {
$this->message = $e->getMessage();
}



I tried also to check for empty fields and invalid email before I check if there is existing user. Tried to clear error before registering but not working.



This question appears to be off-topic. The users who voted to close gave this specific reason:





I guess you need to return true or false according to the checked condition
– Penguine
Jul 2 at 10:17





Where do you mean ?
– merko
Jul 2 at 10:25





Please specify more info abt ur problem
– Penguine
Jul 2 at 10:50





The value from count(*) can be 0, 1, or more. This means the total rows will always be 1 no matter the count value. Think about your code logic. You want to check the value, not the number of rows.
– mickmackusa
Jul 4 at 21:21



count(*)


0


1


1





php.net/manual/en/pdostatement.rowcount.php The manual suggests fetchColumn().
– mickmackusa
Jul 4 at 21:27


fetchColumn()




Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages