2014-04-10 146 views
0

當我嘗試將博客條目添加到我的數據庫中時,我總是收到錯誤。我有一個簡單的語法高亮顯示器,但沒有顯示哪裏不對。PHP解析錯誤:語法錯誤,意外的T_OBJECT_OPERATOR,期待')'

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

} 
+1

'(新的DateTime()) - >格式( 'Y-M-d H:I:S')'<---你在哪裏看到這樣的語法? – zerkms

+0

[PHP解析錯誤:語法錯誤,意外的T \ _OBJECT \ _OPERATOR]的可能重複(http://stackoverflow.com/questions/13388541/php-parse-error-syntax-error-unexpected-t-object-operator) – mario

+0

@zerkms這個語法存在於Laravel中,可能是OP試圖將框架邏輯應用到純php中。 – user2094178

回答

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 
+0

謝謝,那是我的問題。我明白爲什麼這是錯誤的。 – Locke

0

這個工作對我來說:

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