0

I am trying to check an email address against ones listed in a database to make sure it doesn't match any.

I have attempted to use rowCount which I understand returns a bool true or false on whether it found a match.

But the email address still gets entered into the database.

Here is the method I'm using to check the email:

public function checkEmail(){
        if($this->post_data['reg_email'] == $this->post_data['reg_conf_email']){

            if(filter_var($this->post_data['reg_email'], FILTER_VALIDATE_EMAIL)){


                $stmt = $this->dbh->prepare("SELECT email FROM oopforum_users WHERE email = ?");
                $stmt->bindParam(1, $this->post_data['reg_email'], PDO::PARAM_STR);
                $stmt->execute();
                $result = $stmt->rowCount();
                if($this->result){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

Then in my calling program I have this:

if($register->checkUsername()){

    if(!$register->checkEmail()){

          //continue...

    }else{
        $message = 'ERROR: That email is not valid, doesn\'t match the confirmation email or already exists on our database, please choose another one.';
    }


}else{
    $message = 'ERROR: That username is invalid or is already taken, please choose another one and ensure it is atleast 4 characters long.';
}

Why is it not matching the email against the same one in the database and throwing the error message?

crmepham
  • 4,676
  • 19
  • 80
  • 155

2 Answers2

1
<?php
  $email="SELECT count(*) as emailCount FROM oopforum_users 
          WHERE UPPER(email) ='".strtoupper($catid)."'";
  $rs_count=mysql_query($email) or die(mysql_error());
  $result = mysql_fetch_assoc($rs_count);
  if($result['emailCount'] > 0){
     echo "Email Exists !!";
  } else {
     echo "Email Doesn't Exists !!";
  }
?>
manurajhada
  • 5,284
  • 3
  • 24
  • 43
0

I am trying to check an email address against ones listed in a database to make sure it doesn't match any. [...] But the email address still gets entered into the database.

First of all; if your requirement is that you can never have two users with the same e-mail address, I would argue that you'd be better off enforcing that in the database (guessing that you're using MySQL):

ALTER TABLE oopforum_users ADD UNIQUE ( email );

Once you've created a unique constraint in your database, it's impossible to insert an e-mail address twice. That could also be used to your benefit in the code; simply try to insert the row, and if it fails with error code 1062, you know the unique constraint has been violated. This makes your code easier, and less intensive on the database.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58