当我尝试将博客条目添加到我的数据库时,我一直收到错误消息。我有一个简单的语法高亮显示,但它没有显示出某些不对的地方。
错误:
PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' on line 75 and 71
我的剧本:
if(!isset($error)){
try {
//insert into database
$stmt = $db->prepare('INSERT INTO blog_posts (postTitle,postDesc,postCont,postDate) VALUES (:postTitle, :postDesc, :postCont, :postDate)') ; //line 71
$stmt->execute(array(
':postTitle' => $postTitle,
':postDesc' => $postDesc,
':postCont' => $postCont,
':postDate' => (new DateTime())->format('Y-m-d H:i:s') //Line 75
));
//redirect to index page
header('Location: index.php?action=posted&title='.$postTitle.'');
exit;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
答案 0 :(得分:1)
您使用format()是错误的,更改:
...
':postDate' => (new DateTime())->format('Y-m-d H:i:s')
到
...
$date = new DateTime();
$formattedDate = $date->format('Y-m-d H:i:s');
....
':postDate' => $formattedDate
答案 1 :(得分:0)
这对我有用:
$today = (new \DateTime('NOW'))->format('d-m-y');