0

After fixing various issues, this finally returns no PHP errors, but doesn't update the database... Maybe it's the syntax of this piece of code ?

The variables $postId, $id, $comment returns correctly in other functions...

public function updateComment($postId, $id, $comment) {   
    $db = $this->dbConnect();
    $commentUpdate = $db->prepare(
        'UPDATE comments SET comment, comment_date VALUES(:comment, NOW()) 
         WHERE post_id = :post_id AND id = :id'
    );
    $affectedLines = $commentUpdate->execute(array(
        'comment' => $comment,
        'post_id' => $postId,
        'id' => $id,
    ));
    return $affectedLines;
}
GMB
  • 216,147
  • 25
  • 84
  • 135

2 Answers2

1

Your SQL is not valid, you seem to be mixing the UPDATE and INSERTsyntax.

You probably want:

UPDATE comments 
SET comment = :comment, comment_date = NOW()
WHERE post_id = :post_id AND id = :id 

NB: your question indicates that you are not properly checking for errors when running SQL queries. Error checking is a critical part of any application code that interacts with a database, as it helps sorting things out when unexpected behavior happens. You might want to have a look at this SO post for more information on that topic.

GMB
  • 216,147
  • 25
  • 84
  • 135
0

Try with this SQL in the prepared statement

UPDATE comments SET comment = :comment, comment_date = NOW() WHERE post_id = :post_id AND id = :id  

VALUES is typically used for an INSERT, rarely for an UPDATE.

LukStorms
  • 28,916
  • 5
  • 31
  • 45