这个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();
Run Code Online (Sandbox Code Playgroud)
我没有任何错误。一切对我来说都是正确的。
您的查询应为:
INSERT INTO training (courseId, title, startDate, endDate) VALUES
(:postCourse, :postCourseName, :postStartDate, :postEndDate);
Run Code Online (Sandbox Code Playgroud)
要么:
INSERT INTO training
SET courseId = :postCourse,
title = :postCourseName,
startDate = :postStartDate,
endDate = :postEndDate
Run Code Online (Sandbox Code Playgroud)
参见http://dev.mysql.com/doc/refman/5.5/en/insert.html
您应该能够检查如下错误:
$stmt = $dbh->prepare(...);
if (!$stmt) {
var_dump($dbh->errorInfo());
}
Run Code Online (Sandbox Code Playgroud)
要么:
$stmt->execute();
var_dump($stmt->errorInfo());
Run Code Online (Sandbox Code Playgroud)
要么:
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
...
Run Code Online (Sandbox Code Playgroud)
请参阅:http://www.php.net/manual/en/pdo.error-handling.php