duanmei1694 2017-02-23 09:08
浏览 32
已采纳

登录不起作用

I have been trying to figure this out. It is quite easy but I just cant find the mistake. It looks like the mistake is in foreach, because everything above that works. When I fill the inputs and click the Login button, the script wont work.

if (isset($_POST['submit']) AND (!empty($_POST['username']) AND (!empty($_POST['password'])))) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $pass = hash('sha256',$password);

    $select = $db->prepare("SELECT * FROM users WHERE username= :username AND password= :password");
    $select->bindParam(':username', $username);
    $select->bindParam(':password', $pass);
    $select->execute();

    foreach ($select as $data) {
        if ($data['username'] == $username AND $data['password'] == $pass) {
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $data['id'];
            ?>
            <div class="echos">
                    <?php
                    echo "Login completed" . "<br>";
                    echo "<a href='http://jezecek.nostools.cz/'>Homepage</a>";
        }
        else {
            echo "Wrong username or password!" . "<br>";
            echo "<a href='http://jezecek.nostools.cz/include/registration.php'>Try again</a>";
        }       
    }   
}
else {
    echo "Fill all fields!" . "<br>";
    echo "<a href='http://jezecek.nostools.cz/include/login.php'>Try again</a>";
}
?>
</div>

Even if I use fetchAll() like this, it still doesnt work

$select = $db->prepare("SELECT * FROM users WHERE username= :username AND password= :password");
    $select->bindParam(':username', $username);
    $select->bindParam(':password', $pass);
    $select->execute();
    $result = $select->fetchAll();

    foreach ($result as $data) {
        if ($data['username'] == $username AND $data['password'] == $pass) {
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $data['id'];
            ?>
            <div class="echos">
                    <?php
                    echo "Login completed" . "<br>";
                    echo "<a href='http://jezecek.nostools.cz/'>Homepage</a>";
        }
  • 写回答

4条回答 默认 最新

  • double820122 2017-02-23 09:16
    关注

    please fetch data before going to loop

    $select->execute();
    $data=$select->fetchAll();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度