PHP代码总是显示错误的结果

时间:2014-08-11 18:12:00

标签: php mysql

我正在学习PHP并能够创建注册表单。但代码无法正常工作。它始终归于Username exists Try Again的其他陈述。任何帮助表示赞赏,并且非常欢迎任何解释:)

function session() {
    $usn = $_POST['username'];
    $pwd = $_POST['password']; 
    $email = $_POST['Email'];  

    $con=mysqli_connect("********","***********","**********","*********");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM Accounts 
    WHERE username = '$usn'");

    If($result == Null) {
        mysqli_query($con,"INSERT INTO Accounts (username, password, Email)
        VALUES ('$usn', '$pwd','$email')");

        $result = mysqli_query($con,"SELECT * FROM Accounts WHERE username = '$usn'");

        while($row = mysqli_fetch_array($result)) {
            if (($row['password']==$pwd) and ($row['Email']==$email)) {
                echo "Registration Success";
            }
            else {
                echo "Registration Failed";
            }
        }
    }
    else {
        echo "Username Exists Try Again";
    }

    mysqli_close($con);
}

1 个答案:

答案 0 :(得分:6)

$result永远不会为空。您需要检查行数 -

$row_cnt = mysqli_num_rows($result);

如果大于0,则转到您的其他地方。