pierre-michel-5
pierre-michel-5

Reputation: 13

SQL UPDATE query with prepared statement syntax issue, no error message

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

Upvotes: 0

Views: 33

Answers (2)

GMB
GMB

Reputation: 222482

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.

Upvotes: 1

LukStorms
LukStorms

Reputation: 29657

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.

Upvotes: 0

Related Questions