duanqiao1947
duanqiao1947
2015-08-10 04:55
浏览 84
已采纳

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

Trying to create simple PHP login form that takes 'username' and 'password', if matched with 'dbusername' and 'dbpassword' the code should echo "You're Logged In!.

When I run the code, I get no errors.The page goes from login.php to process.php but shows a blank page. Doesn't show: echo "Incorrent username or password!" or "You're Logged In!".

I checked to see if its returning any rows from database. I'm getting 0 rows. But why?! Is my code logic incorrect? Because my database connection works AND I have a username: alex and password: abc in my database named phplogin and in table 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);
?>

Here is my login.php page [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>

</html>

Any ideas?

UPDATE 1: The issue was with my variables conflicting with database.

Now I am getting the following once I login and it has to do with the last statement on my process.php page:

enter image description here

Why is the final else statement printing on screen when Its logging in?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyanlu7380
    douyanlu7380 2015-08-10 05:01
    已采纳

    Change $username and $password to $uname and $pass as they are conflicting with database credentials

    I highlighted in the code where to make changes

    <?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);
    ?>
    

    and in your HTML missing </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>
    
    点赞 评论
  • dpbe81245
    dpbe81245 2015-08-10 05:01
    $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';
    
    点赞 评论
  • dsb0003795
    dsb0003795 2015-08-10 06:58
        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!";
                }
            }
        ?>
    
    点赞 评论

相关推荐