Reputation: 13
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
Reputation: 222482
Your SQL is not valid, you seem to be mixing the UPDATE
and INSERT
syntax.
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
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