2014-04-15 62 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'];