Jay Jay -4 years ago 72
MySQL Question

PHP/MySQL - Email validation not working

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...

Answer Source

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.';
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download