Reputation: 3
I know this has been asked a lot, and I've search and literally tried it all. I'm really close, but can't get it to work.
I'm trying to check if the username or usermail is already in use and display the error accordingly.
<?php
if (isset($_POST['register'])) {
try
{
$checkValidity = $connect->prepare('SELECT username, usermail FROM users WHERE username = :username OR usermail = :usermail');
$checkValidity->bindValue(':username', $username);
$checkValidity->bindValue(':usermail', $usermail);
$checkValidity->execute();
$row = $checkValidity->fetchColumn();
if ($row == $username) {
$errName = 'This username is already taken.';
}
if ($row == $usermail) {
$errMail = 'Email already in use.';
}
}
catch(PDOException $e)
{
// print $e->getMessage();
}
}
?>
It works fine for the username, but for the life of me I can't get the email error to work...
I attempted separate SELECT queries (username/usermail), and the email error would show ONLY if the username error showed.
The email error will never show just by itself.
I've spent one too many hours just trying to get this to work, and I'm slowly losing my mind.
Any indication would be greatly appreciated...!
Thank you...
Upvotes: 0
Views: 58
Reputation: 12085
There is no way to return another column from the same row if you use PDOStatement::fetchColumn() to retrieve data.
your fetching username only you need to use fetch() to get the whole row and compare like this
$row = $checkValidity->fetch();
if ($row['username'] == $username) {
$errName = 'This username is already taken.';
}
if ($row['useremail'] == $useremail) {
$errMail = 'Email already in use.';
}
Upvotes: 0
Reputation: 484
FetchColumn return first column value. You need to use fetch instead. Like
$row = $checkValidity->fetch();
if ($row['username'] == $username) {
$errName = 'This username is already taken.';
}
if ($row['usermail'] == $usermail) {
$errMail = 'Email already in use.';
}
Upvotes: 2