简体   繁体   English

PHP 仅以登录形式从 MySQL DB 获取最后一行

[英]PHP only taking the last row from MySQL DB in login form

I've been stuck on this issue for 3 days now.我已经在这个问题上停留了 3 天。 I'm trying to make a login form (I've already created a register form) and the database is working too.我正在尝试制作一个登录表单(我已经创建了一个注册表单)并且数据库也在工作。 But now while I'm trying to make the login form, I've noticed that PHP only takes the last row from the database.但是现在当我尝试制作登录表单时,我注意到 PHP 只从数据库中获取最后一行。

1

As you can clearly see in the first picture, my database has 3 records.正如您在第一张图片中清楚地看到的那样,我的数据库有 3 条记录。
But when I try to log in on my account, it only lets me log in to the most recently created account, and not the others.但是当我尝试登录我的帐户时,它只能让我登录到最近创建的帐户,而不能登录其他帐户。 Here's my current code:这是我当前的代码:

<div class="login-form">
    <form method="POST">
        <p style="float:left;">
            <input type="email" class="login-input" maxlength="40" name="login-email" id="login-email" placeholder="email" required><span style="color: red;">&nbsp;*</span><br><br>
             <input type="password" class="login-input" maxlength="32" name="login-passw" id="login-passw" placeholder="password" required><span style="color: red;">&nbsp;*</span><br><br>
             <input type="submit" class="btn" name="login-btn">
         </p>
         <?php
         $email = $_POST["login-email"];
         $passw = $_POST["login-passw"];
         $encrypted_passw = md5($passw);

         $sql = "SELECT id, email, passw FROM users";
         $result = $db->query($sql);

         // if (isset($_POST["login-btn"])) {
         //     if ($_POST["login-email"] == $result["email"]) {
         //         echo "<p>Logged in</p>";
         //     } else {
         //         echo "<p>wrong</p>";
         //     }
         // }
         while ($row = $result->fetch_assoc()) {
             $get_email = $row["email"];
             $get_usr = $row["username"];
             $get_passw = $row["passw"];
         }

         if (isset($_POST["login-btn"])) {
             if ($_POST["login-email"] == $get_email && $encrypted_passw == $get_passw) {
                 echo "<p>Logged in</p>";
             } else {
                 echo "<p> wrong</p>";
             }
        }
        ?>
    </form>
</div>

Try this.尝试这个。 First of all I would place the php code above the HTML.首先,我会将 php 代码放在 HTML 上方。

You only need to listen the post param login-btn .您只需要收听 post 参数login-btn Read the other post data into vars and confirm its there before proceeding.将其他帖子数据读入 vars 并在继续之前确认它。

When you poll the DB you dont need to read every record (imagine you have thousands of records, you wouldn't want to pull them all down).当您轮询数据库时,您不需要读取每条记录(想象一下您有数千条记录,您不想将它们全部拉下来)。 Just filter for the supplied email with a where clause.只需使用 where 子句过滤提供的 email 即可。

If the email exists it will return a result with the hashed password.如果 email 存在,它将返回带有哈希密码的结果。 Verify this matches and you are good to go.验证此匹配项,您就可以使用 go。

The issue you're having where the last record in the db is beiung used is becuase in your loop, you are overwriting the var $get_email each time.您在使用数据库中的最后一条记录时遇到的问题是因为在您的循环中,您每次都覆盖了 var $get_email

<?php
if (isset($_POST["login-btn"])) {

    $email = (isset($_POST["login-email"]) ? $_POST["login-email"] : '');
    $passw = (isset($_POST["login-passw"]) ? $_POST["login-passw"] : '');

    if($email != "" && $passw != ""){

        $encrypted_passw = md5($passw);    

        $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
        $stmt = $mysqli->prepare("SELECT email, passw FROM users where email = ?");
        $stmt->bind_param($email);
        $stmt->execute();

        while ($row = $result->fetch_row()) {

            $get_passw = $row["passw"];

            if($encrypted_passw == $row['passw']){
                echo "logged in";
            }else{
                echo 'no match';
            }
        }
    }
}    
?>

<div class="login-form">
    <form method="POST">
        <p style="float:left;">
            <input type="email" class="login-input" maxlength="40" name="login-email" id="login-email" placeholder="email" required><span style="color: red;">&nbsp;*</span><br><br>
            <input type="password" class="login-input" maxlength="32" name="login-passw" id="login-passw" placeholder="password" required><span style="color: red;">&nbsp;*</span><br><br>
            <input type="submit" class="btn" name="login-btn">
        </p>
    </form>
</div>

change your query to this将您的查询更改为此

"SELECT id, email, passw FROM users where email='".$row["email"]."' and password= '".$row["password"]."'" "SELECT id, email, passw FROM users where email='".$row["email"]."' and password='".$row["password"]."'"

you do not need to use foreach for all rows this query return only one row that you need您不需要对所有行使用 foreach 此查询仅返回您需要的一行

Gottem!天哪! I was using array's instead of values我使用的是数组而不是值

<?php            
session_start();
include_once "../php/db_connect.php";
            if (isset($_POST["login-btn"])) {
                
                $email = $_POST["email"];
                $passw = $_POST["passw"];
                $encrypted = md5($passw);
                
                $sql = "SELECT * FROM users WHERE email = '". $email ."'";
                $result = $db->query($sql);
                $get_result = $result->fetch_assoc();

                if ($encrypted == $get_result["passw"]) {
                    echo "<p>Logged in!</p>";
                    $_SESSION["username"] = $get_result["username"];
                    $_SESSION["id"] = $get_result["id"];
                    $_SESSION["email"] = $get_result["email"];
                    Header("Location:../../../");
                } else {
                    echo "<p>Error</p>";
                }
            }
            ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM