douyulv6921 2017-08-29 14:49
浏览 15
已采纳

PHP用户登录系统问题 - 只有最后一个用户可以登录

I am trying to create a login system using php. What I am trying to create is to have a page that ask for username, password, email. Then when the user fill in the fields to go to the next page where the user will be asked to put his username and password in order to login to the system. Moreover, I have created something but is not working properly. The form is submitted correctly and I receive the data into my database but when the user tries to login into the system it will only allow the LAST user that registered, to login with his credentials into the system. For all the other users that are registered also, the login is not working. Please can someone help to solve this issue?

Thanks.

My HTML and PHP Code:

<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <h2>Please sign in with your username<br>
            and password to complete the form... Thanks!!!</h2>
            <form method="post" action="success.php">

               <?php
                    if(isset($_POST['login'])) {

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


                        $query = "SELECT * FROM users_info";

                        $result = mysqli_query($connection, $query);

                        while($row = mysqli_fetch_assoc($result)) {
                            $db_username = $row['user_username'];
                             $db_password = $row['user_password'];
                        }

                        if($username !== $db_username) {
                            echo "Username does not exist";
                        }else if ($password !== $db_password) {
                            echo "Password does not exist";
                        }else {
                            header("Location: form.php");
                        }

                    }
                ?>

                <div class="form-group">
                   <label for="user_username">Enter Username</label>
                    <input type="text" name="user_username" class="form-control" required>
                </div>
                <div class="form-group">
                   <label for="user_username">Enter Password</label>
                    <input type="password" name="user_password" class="form-control" required>
                </div>
                <div class="form-group">
                    <input type="submit" name="login" value="Log In" class="btn btn-success">
                </div>
            </form>

        </div>
    </div>
</div>
  • 写回答

2条回答 默认 最新

  • doulan3966 2017-08-29 14:52
    关注

    You need to limit the query to pull only the user information that is attempting to log in. Note, this doesn't cover the incoming comments about SQL Safety or error reporting. I strongly suggest you do error checking to see if the fetched results are empty, then move forward.

    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <h2>Please sign in with your username<br>
                and password to complete the form... Thanks!!!</h2>
                <form method="post" action="success.php">
    
                   <?php
                        if(isset($_POST['login'])) {
    
                            $username = $_POST['user_username'];
                            $password = $_POST['user_password'];
    
    
                            //Fetch matching user data
                            $query = "SELECT * FROM users_info WHERE user_username = '$username' AND user_password = '$password'";
    
                            $result = mysqli_query($connection, $query);
    
                            while($row = mysqli_fetch_assoc($result)) {
                                $db_username = $row['user_username'];
                                 $db_password = $row['user_password'];
                            }
    
                            if($username !== $db_username) {
                                echo "Username does not exist";
                            }else if ($password !== $db_password) {
                                echo "Password does not exist";
                            }else {
                                header("Location: form.php");
                            }
    
                        }
                    ?>
    
                    <div class="form-group">
                       <label for="user_username">Enter Username</label>
                        <input type="text" name="user_username" class="form-control" required>
                    </div>
                    <div class="form-group">
                       <label for="user_username">Enter Password</label>
                        <input type="password" name="user_password" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <input type="submit" name="login" value="Log In" class="btn btn-success">
                    </div>
                </form>
    
            </div>
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题