God this is so annoying. I have a messagesystem with inbox, outbox, and a conversation view. The conversation view shows both sent and received messages with a specific user, and since I only use one table for the messages (sender_status/receiver_status to control which of the users involved in the message have deleted it, read it etc.)
I have checkboxes next to my list, one for each message, it works fine in my queries both for inbox and outbox:
<input type="checkbox" name="message_delete[]" value="<?= $message['mid']; ?>"/>
I need to apply the messageupdate to the correct status when I try to delete in the conversation-view: Normal query, i.e. inbox, works fine:
$questionmarks = str_repeat("?,", count($mid_array) - 1) . "?";
$st = $db->prepare("UPDATE messages SET sender_status = 'DELETED' WHERE mid IN ($questionmarks)");
$st->execute($mid_array);
Conversation-query, does not work:
$st = $db->prepare("UPDATE messages SET (receiver_status = 'DELETED' WHERE mid IN ($questionmarks) AND to_user = '{$user_id}'), (sender_status = 'DELETED' WHERE mid IN ($questionmarks) AND from_user = '{$user_id}')");
$st->execute( WHAT TO PUT HERE );
I have tried array($mid_array, $mid_array) in the execute which I thought would work. Also several other somewhat stupid solutions that I can´t get to work. I´m probably missing some easy function here but none of the ones i´ve tried have worked, hence my question. Need Help!! thanks in advance.
Try something more like
UPDATE messages
SET
receiver_status = 'DELETED',
sender_status = 'DELETED'
WHERE mid IN ($questionmarks) AND ('$user_id' IN (to_user, from_user))
to get around the multiple where
issue in your original query.
SET
receiver_status = IF(to_user = '$user_id', 'DELETED', receiver_status),
sender_status = IF(from_user = '$user_id', 'DELETED', sender_status)
WHERE mid IN ($questionmarks)
instead, then?
Whoa.
You just can't do that, with prepared statements or without.
It's SQL syntax, not PDO.
So, just run 2 separate queries with 2 separate sets of data.
By the way, for some reason you're not using placeholders in your query - so, you're in danger.