【问题标题】:not functioning in while($row->$statement->fetch()) using php PDO使用 php PDO 在 while($row->$statement->fetch()) 中不起作用
【发布时间】:2021-03-13 03:27:56
【问题描述】:
if(isset($_POST['btnLogin']))
{
$username = $_POST['txtusername'];
$pass_word = $_POST['txtpassword'];
$hashed_password = crypt(sha1($pass_word));


$sqlQuery  = "SELECT * from users WHERE username = :username AND password = :password" ;
$statement = $conn->prepare($sqlQuery);
$statement->execute(array(':username' =>$user , ':password'=>$hashed_password));

while($row->$statement->fetch())
{
    $id       = $row['id'];
    $username = $row['username'];
    $password = $row['password'];

    if(strcmp('$password', '$hashed_password') == 0)
    {
        echo "<script type='text/javascript'>console.log("Sucess");</script>";
    }
    else
    {
        echo "<script type='text/javascript'>console.log("Failed");</script>";
    }

}

}

在这里,我正在使用 PHP PDO 代码实现登录页面。 我以前没有使用 PHP PDO 的经验。我在注册页面上没有任何错误,但不幸的是程序在之后没有执行

while($row->$statement->fetch())

这里是 HTML 代码

<section>

    <div class="container">
        <div class="login-form">
            <h1>Sign In</h1>
            <form id="login-form" method="post" action="">
            <div class="form-group">
                <input type="text" name="txtusername" placeholder="Username" data-validation="required">
            </div>
            <div>
                <input type="password" name="txtpassword" placeholder="Password" data-validation="required">
            </div>
                <input type="submit" name="btnLogin" value="Login">
            </form>
        </div>
    </div>
</section>

PHP 版本:5.3.8

请给我任何解决它的建议

【问题讨论】:

  • 那是因为你的while($row-&gt;$statement 应该是while($row=$statement
  • 认为这是一个印刷错误。
  • @FunkFortyNiner 我确实改变了你上面提到的但它仍然不起作用
  • 对查询php.net/manual/en/pdo.error-handling.php启用错误报告和错误处理 - 密码也应该保存为相同的哈希,但我不知道你为什么使用你现在使用的,为什么不改用password_hash()password_verify() 呢?同时确保密码列足够长以容纳散列。
  • password_hash() 自 PHP 5.5 起可用。如果它不可用,这意味着您想要使用已经 EOL 至少 5 年的代码来处理敏感的用户数据?坏主意!

标签: php pdo while-loop


【解决方案1】:

试试下面的。如果它不起作用,wrap your query in this 并打开错误报告。

if(isset($_POST['btnLogin']))
{
$username = $_POST['txtusername'];
$pass_word = $_POST['txtpassword'];
$hashed_password = crypt(sha1($pass_word));


$sqlQuery  = "SELECT * from users WHERE username = :username AND password = :password" ;
$statement = $conn->prepare($sqlQuery);
$statement->execute(array(':username' =>$user , ':password'=>$hashed_password));

while($row = $statement->fetch_object())
{
    $id       = $row->id;
    $username = $row->username;
    $password = $row->password;

    if(strcmp('$password', '$hashed_password') == 0)
    {
        echo '<script type="text/javascript">console.log("Success");</script>';
    }
    else
    {
        echo '<script type="text/javascript">console.log("Failed");</script>';
    }

}

}

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2013-01-03
    • 2011-02-11
    相关资源
    最近更新 更多