简体   繁体   English

使用PDO登录-Php

[英]Login using PDO - Php

I am implementing a simple user login using PDO. 我正在使用PDO实现简单的用户登录。 But it is giving me a warning message : 但这给了我一个警告信息:

Notice: Undefined index: id in login.php on line 39. 注意:未定义的索引:第39行的login.php中的id。

Code: 码:

<?php
if($_POST)
{

    $query = $db->prepare('SELECT * FROM users 
                           WHERE email = :email and password = :pass');

    $query->execute(array(':email' => $_POST['email'], 
                  ':pass'  => $_POST['password'])); 

    $count  = $query->rowCount();

    if($count==1)
    {
        $result         = $query->fetchAll();

        $_SESSION['user']   = $result['id'];

        header("location:index.php");   
        exit;   
    }
    else
    {
        $_SESSION['message']='Invalid login details.';
        header("location:login.php");   
        exit;   
    }
}
?>

What am I missing here? 我在这里想念什么?

Assuming you store unique usernames. 假设您存储唯一的用户名。 You don't need to use fetchAll() here. 您无需在此处使用fetchAll()。 You can use fetch(); 您可以使用fetch();

$result = $query->fetch(PDO::FETCH_ASSOC);
echo $result['id'];

fetchAll returns an array: http://www.php.net/manual/en/pdostatement.fetchall.php fetchAll返回一个数组: http ://www.php.net/manual/zh/pdostatement.fetchall.php

So you'll need $result[0]['id'] 因此,您需要$result[0]['id']

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

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