PHP注册脚本 - 不工作

时间:2010-02-18 13:05:00

标签: php mysql scripting

我刚刚从头开始编写了一个注册脚本,但我仍然会改进它。基本上我在改进它之前先测试它 - 不知何故,它不起作用......

if(isset($_POST['submit']))
{
   $username = mysql_escape_string(trim($_POST['login']));
   $password = $_POST['password'];
   $vpassword = $_POST['vpassword'];
   $email = mysql_escape_string(trim($_POST['email']));
   $vemail = mysql_escape_string($trim($_POST['vemail']));

   $ip = $_SERVER['REMOTE_ADDR'];
   $date = date("jS F Y");

if(!empty($username) || !empty($password) || !empty($vpassword) || !empty($email) || !empty($vemail))
{
    if($password == $vpassword && $email == $vemail)
    {
        $sql = "SELECT username FROM members WHERE username='" . $username . "'";

        $result = mysql_query($sql) or die(mysql_error());

        if(mysql_affected_rows($result) == 1)
        {
            echo "The account already exists!";
        }
        else
        {
            $sql = "INSERT INTO members
                    (username, password, email, ip, level, type, signed, hide, hidemail, avatar, notes)
                    VALUES('" . $username . "', '" . $password . "', '" . $email . "', '" . $ip . "', '0', '0', '" . $date . "', '1', '1', '', '')";

            $result = mysql_query($sql) or die(mysql_error());
        }
    }
    else
    {
        echo "Please check the fields so that they match";
    }
}
else
{
    echo "Check the missing fields!";
}

if($result)
{
    echo "Account created successfully!";
}
else
{
    echo "Error creating account";
}
}

这可能有什么问题......?

感谢帮助: - )

1 个答案:

答案 0 :(得分:0)

使用||时,您正在检查其中一个值是否为空。请改用&&。这样您就不允许任何字段为空。所以:

if(!empty($username) || !empty($password) || ...

这意味着“如果其中任何一个不为空”。请改用:

if(!empty($username) && !empty($password) && ...

这意味着“如果没有空字段”。