Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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

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

}
share|improve this question
1  
(new DateTime())->format('Y-m-d H:i:s') <--- where did you see such a syntax? – zerkms Apr 10 '14 at 3:10
    
possible duplicate of PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR – mario Apr 10 '14 at 3:17
    
@zerkms this syntax exists in Laravel, probably the OP was trying to apply the framework logic into plain php. – user2094178 Apr 10 '14 at 3:29
    
@zerkms Hi again, in the following link you can find a usage example of the type (new Model)->method(), github.com/laravel/framework/issues/1436 , look for the first comment from May 29. – user2094178 Jun 28 '14 at 20:43

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Thanks, that was my problem. I see why that is wrong. – Locke Apr 10 '14 at 3:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.