PHP解析错误:语法错误,意外的T_OBJECT_OPERATOR,期望“)”

时间:2021-03-16 22:30:58

I keep getting an error when I try to add a blog entry into my database. I have a simple syntax highlighter but it is not showing where something is not right.

当我试图在我的数据库中添加博客条目时,我总是会出错。我有一个简单的语法高亮工具,但它并没有显示哪里有错误。

Error: PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' on line 75 and 71

错误:PHP解析错误:语法错误,意外T_OBJECT_OPERATOR,预期“)”在第75行和第71行。

My script:

我的脚本:

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

}

2 个解决方案

#1


1  

your usage of format() is wrong, change:

格式()使用错误,请更改:

...
':postDate' => (new DateTime())->format('Y-m-d H:i:s') 

to

...
$date = new DateTime();
$formattedDate = $date->format('Y-m-d H:i:s');
....
':postDate' => $formattedDate

#2


0  

This works for me:

这工作对我来说:

$today = (new \DateTime('NOW'))->format('d-m-y');

#1


1  

your usage of format() is wrong, change:

格式()使用错误,请更改:

...
':postDate' => (new DateTime())->format('Y-m-d H:i:s') 

to

...
$date = new DateTime();
$formattedDate = $date->format('Y-m-d H:i:s');
....
':postDate' => $formattedDate

#2


0  

This works for me:

这工作对我来说:

$today = (new \DateTime('NOW'))->format('d-m-y');