2012-08-23 143 views
0

這個PDO插入語句沒有任何理由在MySQL中插入嗎?PDO語句沒有插入

$stmt = $dbh->prepare("INSERT INTO training courseId = :postCourse, title = :postCourseName, startDate = :postStartDate, endDate = :postEndDate"); 
$stmt->bindParam(':postCourse', $postCourse); 
$stmt->bindParam(':postCourseName', $postCourseName); 
$stmt->bindParam(':postStartDate', $postStartDate); 
$stmt->bindParam(':postEndDate', $postEndDate); 
$stmt->execute(); 

我沒有得到任何錯誤。一切對我來說都是正確的。

+1

錯誤的SQL INSERT語法。它應該是這樣的:'INSERT INTO tbl(col1,col2,...)VALUES(:val1,:val2 ...)' – bfavaretto

+0

@bfavaretto不正確,是,錯,可疑...... http:// dev .mysql.com/doc/refman/5.5/en/insert.html –

回答

4

您的查詢應該是:

INSERT INTO training (courseId, title, startDate, endDate) VALUES 
(:postCourse, :postCourseName, :postStartDate, :postEndDate); 

或者:

INSERT INTO training 
SET courseId = :postCourse, 
    title = :postCourseName, 
    startDate = :postStartDate, 
    endDate = :postEndDate 

http://dev.mysql.com/doc/refman/5.5/en/insert.html

您應該能夠檢查錯誤是這樣的:

$stmt = $dbh->prepare(...); 
if (!$stmt) { 
    var_dump($dbh->errorInfo()); 
} 

或者:

$stmt->execute(); 
var_dump($stmt->errorInfo()); 

或者:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
... 

參見:http://www.php.net/manual/en/pdo.error-handling.php

+0

糟糕!謝謝,那就是訣竅! –

4

是的,你錯過了SET

$stmt = $dbh->prepare("INSERT INTO training SET courseId = :postCourse, title = :postCourseName, startDate = :postStartDate, endDate = :postEndDate"); 
+0

(抱歉,彼得,我錯了,評論和downvote刪除) – bfavaretto