2012-11-29 133 views
-1

我將此登錄腳本更改爲PDO。現在它通過用戶名,但得到卡住fetchAll線。我需要幫助。感謝PDO登錄腳本不起作用

<?php 
session_start(); 
include_once"includes/config.php"; 

if (isset($_POST['admin_login'])) { 
    $admin_user = trim($_POST['admin_user']); 
    $admin_pw = trim($_POST['admin_pw']); 

    if ($admin_user == NULL OR $admin_pw == NULL) { 
    $final_report.="Please complete all the fields below.."; 
    } else { 
    $check_user_data = $db->prepare("SELECT * FROM `admin` 
     WHERE `admin_user`='$admin_user'"); 
    $check_user_data->execute(); 

    if ($check_user_data->fetchColumn() == 0) { 
     $final_report.="This admin username does not exist.."; 
    } else { 
     $get_user_data = $check_user_data->fetchAll($check_user_data); 

     if ($get_user_data['admin_pw'] == $admin_pw) { 
     $start_idsess = $_SESSION['admin_user'] = "".$get_user_data['admin_user'].""; 
     $start_passsess = $_SESSION['admin_pw'] = "".$get_user_data['admin_pw'].""; 
     $final_report.="You are about to be logged in, please wait a few moments..."; 
     header('Location: admin.php'); 
     } 
    } 
    } 
} 
?> 

回答

2
  • 不檢查返回值準備()或執行()爲false。您需要檢查SQL錯誤並處理它們,停止代碼而不是繼續。

  • 不在預處理語句中使用查詢參數,仍然不安全地插入$ _POST內容到查詢中。您錯過了切換到PDO的好處,並使自己容易受到SQL注入攻擊。

  • 您正在以明文存儲密碼,這是不安全的。請參閱You're Probably Storing Passwords Incorrectly

  • 你真的需要SELECT *如果你只使用admin_pw列?提示:不。

  • PDOStatement::fetchAll()返回一個數組數組,而不僅僅是一個數組。閱讀fetchAll()的文檔中的示例。

+0

我是新的PDO,我應該如何改變它的工作原理。謝謝 – user1858570

+0

@ user1858570閱讀一些教程可幫助您掌握PDO的優勢。把我的頭頂部(當然,收藏夾列表,但無論如何),我可以推薦http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers –

+0

感謝,它看起來非常有用的文章,但我不知道如果我能得到我的腳本改變了我需要的方式。 – user1858570