这个PHP / MySQL代码有什么问题?

时间:2012-03-30 05:52:25

标签: php mysql mysql-num-rows

        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!";

    }

我一直收到的错误是“该电子邮件已在使用中,请使用其他电子邮件注册。”即使正确的文件是“必需的”并正确连接到数据库。问题最有可能发生在$ numrows_checkemail部分,因为当我使用if($numrows_checkemail == 0)时,它的工作正常。为什么不“>”符号工作? 难道我做错了什么? 谢谢

2 个答案:

答案 0 :(得分:1)

仅当$ numrows_checkemail大于0时,

if($numrows_checkemail > 0)才会返回true  您需要检查$numrows_checkemail == 0empty($numrows_checkemail)

答案 1 :(得分:1)

>正在扭转你的逻辑;

如果数据库中至少有一位拥有该电子邮件的用户,则

$numrows_checkemail > 0为真(即如果数据库中包含该电子邮件的行数超过零)

$numrows_checkemail == 0如果数据库中没有该电子邮件的用户,则为真(如果该电子邮件中没有任何数据行)