登录系统未完全正常工作 - 字符串输出错误

时间:2015-12-16 22:40:13

标签: php html mysql

所以我有一个登录和创建帐户表单。创建帐户系统完美运行,将所有信息发送到mySQL数据库。

现在,我编写了一个'login_user.php'脚本,它连接到数据库,获取注册用户的值,并根据正确或不正确的用户输入输出正确的消息。看起来操作贯穿整个代码并且每次都输出最后一条消息“无效的用户名或密码”,即使没有输入或输入了错误的用户名/密码。下面我将提供我所有的登录表单php代码。你能发现任何错误吗?如果您想要引用html代码的特定部分,请告诉我。

<?php

$username = $_POST['username'];
$password = $_POST['password'];

if ($username && $password) {

    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
    $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

    $query = mysql_query("SELECT * FROM Client_Information WHERE username='$username'");
    $numrows = mysql_num_rows($query);

    if($numrows != 0){

        while($row = mysql_fetch_assoc($query)){

            $dbusername = $row['username'];
            $dbpassword = $row['password'];
        }

        if($username==$dbusername){
            if($password==$dbpassword){

                echo "You are logged in.";

            }else{
                echo "Invalid password.";
            }

        }else{
            echo "Invalid username.";
        }

    }else{
        echo "This name does not exist.";
    }

}else{
    echo "Invalid username or password.";
}

?>

HTML CODE:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="chrome=1,IE=edge" />
    <title>LOGIN</title>
    <link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
    <div class="logo"></div>
    <div class="login-block">
        <h1>Log In</h1>
        <form action="login_check.php" method="post">
            <input type="text" value="" placeholder="Username" id="username" name="username" />
            <input type="password" value="" placeholder="Password" id="password" name="password" />
            <button>Log In</button>
            <a href="url">Sign Up for New Account?</a>
        </form>
    </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

对PHP使用$_POST时,您必须引用输入的name属性,而不是id属性。

<input type="text" value="" placeholder="Username" id="name" name="name" />
<input type="password" value="" placeholder="Password" id="password" name="password" />