I am making a chatbox IM.
I have a table where I want to update if the user closes the window, meaning he has viewed the messages his friend has sent him.
When this happens, each conversation has a unique id so I find messages with that id and set the viewed column equal to the users name.
However if the other user also closes the window I would like to change the viewed column to ‘both’ for all the messages so none of those messages pops up for either user. (by default the viewed column is equal to 0).
What I want is a query that just finds all the messages with the convo_id and if that message is already viewed by another user meaning viewed!=0 then it is set to both else it is set to the user closing the box.
I have a query but it doesn’t work because I didn’t quite get the syntax right.
Just wondering if anyone could correct it or suggest something.
$sql = "UPDATE chatbox IF (viewed!=?) THEN
SET viewed=? WHERE convo_id=?
ELSE
SET viewed=? WHERE convo_id=?";
$stmt18 = $conn->prepare($sql);
$result = $stmt18->execute(array(0,$receiver,$convo_id,'both',$convo_id));
you’ll want to use a case statement: