2014-04-15 63 views
0

我正在使用PDO实现简单的用户登录。但它给我一个警告消息:使用PDO登录 - Php

注意:未定义指数:ID中的login.php在线39

代码:

<?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; 
    } 
} 
?> 

缺少什么我在这里?

+2

[PHP: “注意:未定义变量” 和 “通知:未定义的索引”]的可能重复(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice -dedefined-index) – Gajus

+0

fetchAll意味着多个行 – Luceos

+0

使用''var_dump($ result)''来查看PDO返回的结果。 –

回答

2

假设你存储唯一的用户名。这里不需要使用fetchAll()。你可以使用fetch();

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