idris
idris

Reputation: 1049

Updating read column when user sees notification

So I have a notification system, and want to check if the user read their notification. So I added a read row. If the value is 0 it's unread, and the other way around for it being read. So in my foreach loop, that displays the notification This is how I mark it as read

//Mark as read
$mkrd = $con->prepare("UPDATE notifications SET read = 1 WHERE id = :hid;");
$mkrd->bindValue(':hid', $notification['id']);
$mkrd->execute();

I believe that the loop will loop, when the user looks at it, and then it'll update to 1. Now here's the issue, for some reason it won't update. No PHP errors, or MYSQL errors either. I'm sure I'm doing everything right, any ideas? Also any better way of doing this?

Here's the loop

foreach($notifications as $notification) {
     $notification;
     $readntf = 1;
     //Mark as read
     $mkrd = $con->prepare("UPDATE notifications SET read = 1 WHERE id = :hid;");
     $mkrd->bindValue(':hid', $notification['id']);
     $mkrd->execute();
echo $notification['type'];
}

Upvotes: 1

Views: 839

Answers (1)

Darren
Darren

Reputation: 13128

Your issue is that the column read is a Reserved Word.

What you want to do is wrap that in back ticks like this:

 $mkrd = $con->prepare("UPDATE notifications SET `read` = 1 WHERE id = :hid");

Side Note: You don't really need that closing comma (;) in your query :-)

Upvotes: 1

Related Questions