2013-11-04 56 views
0

我是PDO的新手,不斷收到致命錯誤。我正在嘗試首先檢查空字段,然後檢查重複的電子郵件,然後如果該通行證將用戶數據插入到數據庫中。在搜索和搜索之後,我絕對失去了我錯誤的地方。這裏是我的代碼:致命錯誤:調用一個非客觀的成員函數execute()

<?php 
session_start(); 

require_once('includes/db_connect.php'); 
include('functions/email-inject-function.php'); 

$first_name = trim($_POST['first_name']); 
$last_name = trim($_POST['last_name']);  
$email = trim($_POST['email']); 
$company = trim($_POST['company']); 
$phone = trim($_POST['phone']); 
$password = trim($_POST['password']); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

if(empty($_POST["first_name"])) { 
    $first_name_err = "<p>What is your first name?</p>"; 
    $errorflag = 1; 
} 
if(empty($_POST["last_name"])) { 
    $last_name_err = "<p>What is your last name?</p>"; 
    $errorflag = 1; 
} 
//checks email 
if(empty($_POST["email"])) { 
    $email_err = "<p>What is your email address?</p>"; 
    $errorflag = 1; 
} 
    if(empty($_POST["company"])) { 
    $company_err = "<p>What is your company name?</p>"; 
    $errorflag = 1; 
} 
    if(empty($_POST["phone"])) { 
    $phone_err = "<p>What is your phone number?</p>"; 
    $errorflag = 1; 
} 
    if(empty($_POST["password"])) { 
    $pass_err = "<p>Please enter a password</p>"; 
    $errorflag = 1; 
} 
    else { 
    $injected = IsInjected($email); 
    if ($injected == true) { 
    $email_valid_err = "<p>Please enter a valid email.</p>"; 
    $errorflag = 1; 
    } 
} 
try { 
    // Check if email is taken 
    $stmt = $dbh->prepare("SELECT * FROM `admins` WHERE `email` = :email"); 
    $stmt->execute(array('email' => $email)); 
    if ($stmt->fetchColumn() > 0) { 
    throw new Exception("That email is already taken."); 
    } 
    $sql="INSERT INTO admins (first_name, last_name, email, company, phone, password, reg_date) VALUES (:first_name, :last_name, :email, :company, :phone, SHA1('$password'), NOW())"; 
    $query = $dbh->prepare($sql); 
    $result->execute(array(':first_name'=>$first_name, ':last_name'=>$last_name, ':email'=>$email, ':company'=>$company, ':phone'=>$phone, ':password'=>$password)); 
    echo $result; 

    //catch any errors from try() 
    } 
    catch(PDOException $e) 
    { 
    echo $e->getMessage(); 
    } 
} 
?> 

回答

3

它是一個簡單的錯誤:

取代$result$query ....

所以:

$result->execute(array(':first_name'=>$first_name, ':last_name'=>$last_name, ':email'=>$email, ':company'=>$company, ':phone'=>$phone, ':password'=>$password)); 
echo $result; 

應該是:

$query->execute(array(':first_name'=>$first_name, ':last_name'=>$last_name, ':email'=>$email, ':company'=>$company, ':phone'=>$phone, ':password'=>$password)); 
echo $query; 

查詢也是錯誤的:

$sql="INSERT INTO admins (first_name, last_name, email, company, phone, password, reg_date) VALUES (:first_name, :last_name, :email, :company, :phone, SHA1('$password'), NOW())"; 

應該

$sql="INSERT INTO admins (first_name, last_name, email, company, phone, password, reg_date) VALUES (:first_name, :last_name, :email, :company, :phone, SHA1(:password), NOW())"; 

注意$密碼:密碼

+0

什麼會是回聲_echo back_? – geomagas

+0

不要問我,但$結果永遠不會分配... –

+0

非常感謝你@geomagas。現在它給了我這個:警告:PDOStatement :: execute()[pdostatement.execute]:SQLSTATE [HY093]:無效的參數編號:綁定變量的數量不匹配 – tonjaggart

相關問題