2014-10-01 143 views
0

我試圖插入使用PDO一些數據,如低於PDO插入查詢不插入數據

$sql = "INSERT INTO tbl_category SET `category_title` = :cat_name , `category_alias` = :category_alias , `category_status`= :cat_status, `category_parent_id` = $parent_id, " 
      . "category_description = '$cat_description'"; 
    $statement = $this->db->conn_id->prepare($sql); 

    $statement->bindParam(':cat_name', $cat_name, PDO::PARAM_STR); 
    $statement->bindParam(':cat_status', $cat_status, PDO::PARAM_STR); 
    $statement->bindParam(':category_alias', $category_alias, PDO::PARAM_STR); 
    $statement->bindParam(':parent_id', $parent_id, PDO::PARAM_INT); 


if ($statement->execute()) { 
     echo "executed"; exit; 
     return $this->db->conn_id->lastInsertId(); 
    } else { 
     echo "not executed"; exit; 
    } 

它總是顯示我「不執行」,但是當我手動運行查詢,它工作正常

+0

沒有它的問題,我們可以在'insert'查詢中使用'set',或者你說'PDO'不允許它? – baig772 2014-10-01 20:19:33

+0

然後,如果你還沒有這樣做,使用'setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION)'。它會概述你的錯誤。你顯然沒有檢查錯誤。 – 2014-10-01 20:20:22

+0

已經試過,它不顯示任何東西 – baig772 2014-10-01 20:22:57

回答

0

問題是你在混合綁定和字符串。

綁定

$parent_id 
$cat_description 

你有錯別字我的朋友。清理你的代碼。

$sql = "INSERT INTO tbl_category SET `category_title` = :cat_name , `category_alias` =  :category_alias , `category_status`= :cat_status, `category_parent_id` = :parent_id, category_description = :cat_description"; 
+0

爲此,我面臨以下問題:(http://stackoverflow.com/questions/26069715/pdo-parameter-is-not-binding – baig772 2014-10-01 20:24:39