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


        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)时它工作得很好。为什么">"符号不起作用?我做错了什么吗?谢谢

只有当 $numrows_checkemail 大于 0 时,if($numrows_checkemail > 0)才会返回true
您需要检查$numrows_checkemail == 0empty($numrows_checkemail)

>正在颠倒你的逻辑;

如果数据库中

至少存在一个使用该电子邮件的用户(即,如果数据库中使用该电子邮件的行数超过零(,则$numrows_checkemail > 0为 true(

如果数据库中

不存在使用该电子邮件的用户(即,如果数据库中没有任何包含该电子邮件的行(,则$numrows_checkemail == 0为 true(

最新更新