2014-10-01 139 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