简单的PHP登录表单返回' 0' mysqli_num_rows。我的代码逻辑出了什么问题?

时间:2015-08-10 04:55:07

标签: php mysqli login

尝试创建简单的PHP登录表单,其中包含' username'和'密码',如果与'dbusername''dbpassword'匹配代码应为echo "You're Logged In!.

当我运行代码时,我没有收到任何错误。页面从login.php转到process.php但显示空白页面。不显示:echo" Incorrent用户名或密码!"或者"您已登录!"。

我检查了它是否从数据库返回任何行。我得到了0行。但为什么?!我的代码逻辑不正确吗?因为我的数据库连接有效并且我的数据库中有username: alex and password: abc名为phplogin和表users

   <?php

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

$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'phplogin';

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT *
                      FROM users
                      WHERE username = '".$username."'
                      AND password = '".$password."'";

$result = mysqli_query($conn, $sql);

echo mysqli_num_rows($result); //   I Checked to see if I was getting no rows. *I am getting 0 rows!!* But why?!


if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {

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

        //check to see if the match

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

        echo "You're Logged In!";

 } if($username != $dbusername || $password != $dbpassword) {
        echo "Incorrect password or username!";
} else {
        die("That user doesn't exist!");
}

}

}

mysqli_close($conn);
?>

这是我的login.php页面[表格]

<html>

    <form action="process.php" method="POST">
    Username:   <input type="text" name="username"><br>
    Password:   <input type="password" name="password"><br>
        <input type="submit" value="Log In"><br>

</html>

有什么想法吗?

更新1:问题在于我的变量与数据库冲突。

现在我登录后会收到以下信息,它与我的process.php页面上的最后一个语句有关:

enter image description here

为什么登录时会在屏幕上打印最终的else语句?

3 个答案:

答案 0 :(得分:3)

$username$password更改为$uname$pass,因为它们与数据库凭据冲突

我在代码中突出显示了要进行更改的位置

<?php
$uname = mysqli_real_escape_string($_POST['username']); //Change here
$pass = mysqli_real_escape_string($_POST['password']); //Change here

$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'phplogin';

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

//Change Here
$sql = "SELECT * FROM users
            WHERE username = '".$uname."'
            AND password = '".$pass."'"; 

$result = mysqli_query($conn, $sql);

echo mysqli_num_rows($result); //   I Checked to see if I was getting no rows. *I am getting 0 rows!!* But why?!

if (mysqli_num_rows($result) > 0) {
    // output data of user
    $row = mysqli_fetch_assoc($result);
    $dbusername = $row['username'];
    $dbpassword = $row['password'];

    //check to see if the match
    if($uname == $dbusername && $pass == $dbpassword ) { //Change Here
        echo "You're Logged In!";
    } if($uname != $dbusername || $pass != $dbpassword) { //Change Here
        echo "Incorrect password or username!";
    }

} else {
        die("That user doesn't exist!");
}
mysqli_close($conn);
?>

并且您的HTML中缺少</form>

<html>

    <form action="process.php" method="POST">
        Username:   <input type="text" name="username"><br>
        Password:   <input type="password" name="password"><br>
        <input type="submit" value="Log In"><br>
    </form>

</html>

答案 1 :(得分:0)

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

$servername = 'localhost';
$username = 'root'; //change this to another name like $db_username
$password = ''; //change this to another name like $db_password
$dbname = 'phplogin';

答案 2 :(得分:0)

    Please create a notepade++ dbconnection.php file and copy this code

           <?php
                mysql_connect("localhost","root","");
                mysql_select_db("dabasename");
            ?>
    ----------------------------------------------------------

    Please create a notepade++ index.php file and copy this code

        <!DOCTYPE HTML>
        <html lang="en-US">
        <head>
            <meta charset="UTF-8">
            <title></title>
            <meta name = "viewport" content = "width=device-width,initial-                      scale=1" />
            <link rel="shortcut icon" type = "image/jpg" href="images.jpg" />
            <link rel="stylesheet" type = "text/css" href = "css/style.css" />
            <link rel="stylesheet" type = "text/css" href = "css/bootstrap.css" />
            <link rel="stylesheet" type = "text/css" href = "css/bootstrap.min.css" />
        </head>
        <body>
            <div id = "container">
             <div id = "login_header">
              <div id = "login_text">YOur Title</div>
             </div>
             <div id = "login_content">
                <img src = "images.jpg" width = "100%" height = "150px" />
                <table width = "100%" style = "margin-top:25px;">
                    <form name = "frmLogIn" action = "login_check.php" method = "post">
                        <tr>
                            <td width = "10%"></td>
                            <td width = "20%">User Type</td>
                            <td width = "10%">:</td>
                            <td width = "50%">
                             <select name = "optUserType" class = "form-control">
                              <option></option>
                              <option>Admin</option>
                              <option>User</option>
                              <option>Guest</option>
                             </select>
                            </td>
                            <td width = "10%"></td>
                        </tr>

                        <tr>
                            <td width = "10%"></td>
                            <td width = "20%">Username</td>
                            <td width = "10%">:</td>
                            <td width = "50%"><input type = "text" name = "txtUsername" class = "form-control" /></td>
                            <td width = "10%"></td>
                        </tr>
                        <tr>
                            <td width = "10%"></td>
                            <td width = "20%">Password</td>
                            <td width = "10%">:</td>
                            <td width = "50%"><input type = "password" name = "txtPassword" class = "form-control" /></td>
                            <td width = "10%"></td>
                        </tr>
                        <tr><td colspan = "5">&nbsp;</td></tr>
                        <tr><td colspan = "5" align = "center"><input type = "submit" name = "btnLogIn" value = "Log In" class = "btn btn-info" style = "width:100px;"/></td></tr>
                    </form>
                </table>
             </div>
             <div id = "login_footer"></div>
            </div>
        </body>
        </html>

     -------------------------------------------------------------------
     Please create a notepade++ login_check.php file and copy this code 
    <?php
        session_start();
        require('dbconnection.php');
        if(isset($_POST['btnLogIn']))
        {
            $userType = mysql_real_escape_string($_POST['optUserType']);
            $username = mysql_real_escape_string($_POST['txtUsername']);
            $password = mysql_real_escape_string($_POST['txtPassword']);
            $sql = "SELECT * FROM `tbl_user` WHERE user_type = '$userType' AND username = '$username' AND password = '$password'";
            $query = mysql_query($sql);
            $row = mysql_fetch_array($query);
            if(mysql_affected_rows())
            {
                $_SESSION['user_code'] = $row['username'];
                $_SESSION['password'] = $row['password'];
                //header('Location:home.php');
                echo "you have logged in";
            }
            else
            {
                //header('Location:home.php');
               echo "you coud not logged in!";
            }
        }
    ?>