2013-10-07 100 views
1

我有一個問題,我不能使用PDO插入任何東西到MySQL數據庫。無法使用PDO插入到MySQL數據庫....沒有錯誤

我得到沒有錯誤,但每當我檢查數據庫,如果該行已被插入,表是空的。

我知道我有一個連接到數據庫,因爲我可以選擇但不能插入。

這裏是我的類entends PDO

class Database extends PDO 
{ 

    public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) 
    { 
     parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS); 

     //parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTIONS); 
    } 

    /** 
    * select 
    * @param string $sql An SQL string 
    * @param array $array Paramters to bind 
    * @param constant $fetchMode A PDO Fetch mode 
    * @return mixed 
    */ 
    public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) 
    { 
     $sth = $this->prepare($sql); 
     foreach ($array as $key => $value) { 
      $sth->bindValue("$key", $value); 
     } 

     $sth->execute(); 
     return $sth->fetchAll($fetchMode); 
    } 

    /** 
    * insert 
    * @param string $table A name of table to insert into 
    * @param string $data An associative array 
    */ 
    public function insert($table, $data) 
    { 
     /*ksort($data); 

     $fieldNames = implode('`, `', array_keys($data)); 
     $fieldValues = ':' . implode(', :', array_keys($data)); 

     $sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)"); 

     foreach ($data as $key => $value) { 
      $sth->bindValue(":$key", $value); 
     }*/ 

     $sth = $this->prepare("INSERT INTO user (`login`, `password`, `role`) VALUES (:login, :password, :role)"); 

     $sth->bindValue(':login', 'username'); 
     $sth->bindValue(':password', 'password'); 
     $sth->bindValue(':role', 'owner'); 

     $sth->execute(); 

     /*if ($sth->errorCode() != 0) { 

      $arr = $sth->ErrorInfo(); 
      throw new Exception('SQL failure:'.$arr[0].':'.$arr[1].':'.$arr[2]); 
     }*/ 

     $sth->debugDumpParams(); 
    } 

這裏是我的表結構(保持它的簡單調試)。

CREATE TABLE IF NOT EXISTS `user` (
    `userid` int(11) NOT NULL AUTO_INCREMENT, 
    `login` varchar(25) NOT NULL, 
    `password` varchar(64) NOT NULL, 
    `role` enum('default','admin','owner') NOT NULL DEFAULT 'default', 
    PRIMARY KEY (`userid`) 
) ENGINE=InnoDB 

編輯:

我找到了問題的癥結所在。問題出在數據數組上。如果我在調用insert()函數時使用$ data ['first_name'],它會失敗,但如果我用硬編碼值替換所有$ data []值,它就會工作,所以問題出在$ data []

創建POST變量

// get all the post data from the registration form 
$data = array();   
$data['first_name'] = $_POST['first_name']; 
$data['last_name'] = $_POST['last_name']; 
$data['gender'] = $_POST['gender']; 
$data['email'] = $_POST['email']; 
$data['interests'] = $_POST['interests']; 
$data['username'] = $_POST['username']; 
$data['password'] = $_POST['password']; 
$data['newsletter'] = $_POST['newsletter']; 
$data['terms'] = $_POST['terms']; 
$data['captcha'] = $_POST['captcha']; 

這被傳遞到創建函數

public function create($data) { 
    $this->db->insert('users', array(
    'first_name' => $data['first_name'],  //this won't work 
    'first_name' => 'whatever the name is', //this will work 
    'last_name' => $data['last_name'], 
    'email' => $data['email'], 
    'username' => $data['username'], 
    'password' => $password, 
    'user_salt' => $user_salt, 
    'sex' => $data['gender'], 
    'interests' => $data['interests'], 
    'signup_date' => date('Y-m-d H:i:s'), 
    'verification_code' => $code 
    )); 

的陣列,並且這是在它被插入

public function insert($table, $data) 
{ 
    ksort($data); 

    $fieldNames = implode('`, `', array_keys($data)); 
    $fieldValues = ':' . implode(', :', array_keys($data)); 

    $sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)"); 

    foreach ($data as $key => $value) { 
     $sth->bindValue(":$key", $value); 
    } 

    $sth->execute(); 
} 
+0

您的數據庫用戶必須插入記錄的權限? –

+0

我怎麼知道?我剛剛嘗試使用另一個數據庫類進行插入,它插入罰款,所以我沒有看到問題是什麼 – AdRock

+0

而你沒有得到任何錯誤?通常這意味着INSERT本身非常成功。你可能想要確保你的代碼不會刪除你剛纔插入或刪除的行,並在插入後重新創建表。你也應該確保你實際選擇了正確的數據庫。或者你可能會使用錯誤的HOST。 –

回答

0
$fieldValues = implode(':,', array_keys($data)); 
0

試試這個,

$fieldNames = implode(', ', array_keys($data)); 
$fieldValues = ':' . implode(', :', array_values($data)); 

$sth = $this->prepare("INSERT INTO $table ($fieldNames) VALUES ($fieldValues)"); 
+0

我試過了,但是我收到錯誤'連接失敗:SQLSTATE [HY093]:無效的參數編號:沒有參數bound1'。我的功能可以在這個貼上找到:http://pastie.org/8612517。任何想法爲什麼? –

相關問題