2013-02-19 100 views
0

我打算清理代碼,並使其更多的OOP友好以後,但現在我努力讓這個工作。我已經設法讓它回聲'嗨',但執行似乎沒有把任何東西放入數據庫,並沒有給我任何錯誤。代碼是問題與表格和PDO

public function newAccount(array $data) { 
    $error = NULL; 
    //Check first name length, and make sure its over 2 characters 
    if (strlen($data['fname']) > 2) { 
     $fname = $data['fname']; 
    } 
    else { 
     $fname = FALSE; 
     $error .= "Please put in a valid First Name. <br />"; 
    } 

    //Check if last name length is over 2 characters 
    if (strlen($data['lname']) > 2) { 
     $lname = $data['lname']; 
    } 
    else { 
     $lname = FALSE; 
     $error .= "Please enter a valid Last Name. <br />"; 
    } 

    // Check username 
    if (strlen($data['user']) > 3) { 
      $user = $data['user']; 
    } 
    else { 
     $user = FALSE; 
     $error .= "Username must be longer than 3 characters.<br />"; 
    } 

    // Mske sure password is atleast 6 characters, and retyped correctly 
    if (strlen($data['pass']) > 5) { 
     if ($data['pass'] == $data['repass']) { 
      $pass = $data['pass']; 
     } 
     else { 
      $pass = FALSE; 
      $error .= "Passwords do not match.<br />"; 
     } 
    } 
    else { 
     $pass = FALSE; 
     $error .= "Password must be longer than 6 characters."; 
    } 

    //make sure email looks correct, strpos makes sure there is an '@' 
    if (strlen($data['email']) > 5 && strpos($data['email'], '@')) { 
     $email = $data['email']; 
    } 
    else { 
     $email = FALSE; 
     $error .= "Please enter a valid email. <br />"; 
    } 
    // Check if user is suppose to be admin 
    if (isset($data['admin'])) { 
     $admin = '1'; 
    } 
    else { 
     $admin = '0'; 
    } 

    if ($fname && $lname && $user && $pass && $email) { 
     echo 'hi'; 
     try { 
     $sth = $this->dbc->prepare("INSERT INTO users(user, password first_name, last_name, email, admin) VALUES(:user, MD5(:pass), :fname, :lname, :email, :admin)"); 

     $sth->execute(array(":user" => $user, 
          ":pass" => $pass, 
          ":fname" => $fname, 
          ":lname" => $lname, 
          ":email" => $email, 
          ":admin" => $admin) 
        ); 
     } 
     catch (PDOException $e) { 
      echo $e->getMessage(); 
     } 

    } 
    else { 
     echo "Error" . $error; 
    } 

} 

在此先感謝!

+0

這將是很好,讓你的錯誤,它給你。我的直覺反應可能是像'用戶'這樣的列需要正確引用。 – 2013-02-19 03:10:06

+0

您是否啓用了PDO :: ERRMODE_EXCEPTION報告? – mario 2013-02-19 03:10:26

+1

這裏缺少逗號'密碼first_name'。 – hjpotter92 2013-02-19 03:11:40

回答

1

在插入查詢中,您在密碼字段後缺少逗號。

應該

$sth = $this->dbc->prepare("INSERT INTO 
    users(user, password, first_name, last_name, email, admin) 
    VALUES(:user, MD5(:pass), :fname, :lname, :email, :admin)"); 

此外,當測試輸入的字符串是電子郵件地址,或不使用filter_var()。像這樣:

if(filter_var($data['email'], FILTER_VALIDATE_EMAIL) { 
    //do this...