PDO语句没有插入

时间:2012-08-23 22:23:46

标签: php pdo

这个PDO插入语句没有插入MySQL的原因吗?

$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();

我没有收到任何错误。一切看起来都对我不错。

3 个答案:

答案 0 :(得分:4)

是的,你错过了SET

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

答案 1 :(得分: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

答案 2 :(得分:3)

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

检查您的INSERT语法http://dev.mysql.com/doc/refman/5.1/en/insert.html