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

简单的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 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办