Search code examples
phpmysqlmysql-num-rows

What is wrong with this PHP/MySQL code?


        if(!empty($username) && !empty($email) && !empty($password) && !empty($confirm_password)){
        $username = htmlentities($username);
        $username = stripslashes($username);
        $username = strip_tags($username);
        $username = mysql_real_escape_string($username);
        $username = preg_replace("[^A-Za-z0-9]", "", $username);

        $email = htmlentities($email);
        $email = stripslashes($email);
        $email = strip_tags($email);
        $email = mysql_real_escape_string($email);
        $email = preg_replace("[^A-Za-z0-9]", "", $email);

        if(strstr($email, "@") && strstr($email, ".")) {
            require("$baseURL/scripts/connect.php");
            $checkemail = mysql_query("SELECT * FROM users WHERE email='$email'") or die(mysql_error());
            $numrows_checkemail = mysql_num_rows($checkemail);
            if($numrows_checkemail > 0) {
                require("$baseURL/scripts/connect.php");
                $checkusername = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
                $numrows_checkusername = mysql_num_rows($checkusername);
                if($numrows_checkusername > 0) {
                    if($password == $confirm_password) {
                    $hashpass = md5(md5($password));
                        //All set to insert into the db
                        require("$baseURL/scripts/connect.php");
                        mysql_query("INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$hashpass')") or die(mysql_error());

                        $this->noticeMsg = "You have been signed up successfully!";
                    } else {
                        $this->errorMsg = "Uh-oh, looks like your passwords do not match!";

                    }
                } else {
                    $this->errorMsg = "Oops, looks like that username is already in use! Please pick a different username.";

                }
            } else {
                $this->errorMsg = "That email is already in use, please sign up with another email.";

            }
        } else {
            $this->errorMsg = "Please enter a valid email address!";

        }
    } else {
        $this->errorMsg = "Please fill in all the fields!";

    }

The error I keep getting is "That email is already in use, please sign up with another email." even though the right file is being "required" and is connected to the database properly. The problem is most likely at the $numrows_checkemail part because when I use if($numrows_checkemail == 0) it works just fine. Why won't the ">" symbol work? Am I doing something wrong? Thank you


Solution

  • if($numrows_checkemail > 0) will return true only if $numrows_checkemail is bigger than 0.
    You need to check for $numrows_checkemail == 0 or empty($numrows_checkemail)