Side
Side

Reputation: 1751

mysql database logic for messages and removing messages

I am currently stuck with my logic.

I have created a database table for snedimg messages

table messages looks like this

id | sender_id | reciver_id | msg                      | send_date |
1  | 2         |  6         | hello there i am testing | 2013-01-15 

So what i am totally stuck with is the removing the message logic. The problem what i dont understand how to solve is, lets say user with the id 6 gets the message, and removes it by id, that way who sent the message wont be able to see it either.

So i am a bit lost at this, if someone could give me a hint on a logic i would really be happy

EDIT WITH MORE EXPLAINED DETAILS

So the problem is

id | sender_id | reciver_id | msg                      | send_date |
1  | 2         |  6         | hello there i am testing | 2013-01-15 

this is the table. User recives the message, reads it, and for some reason wants to remove it.

Probmel is the DELETE LOGIC, i know how to delete rows, what i dont know is how to solve the problem if the reciver deletes the message, the sender will be able to see it un the he/she deletes it.

For example on facebook they use archive instead of delete, this is the logic what i dont understand

Upvotes: 3

Views: 1056

Answers (2)

chill0r
chill0r

Reputation: 1129

Just set the reciver or sender id to -1 on deletion and have a check afterwards if both are -1 (or would be -1) delete that row.

e.g.

on deletion by reciver:
if ($sender_id == -1) {
  $query = "DELETE FROM messages WHERE id=$id";
} else {
  $query = mysql_query("UPDATE messages SET reciver_id=-1 WHERE id=$id";
}
mysql_query($query)

That way it would be still availabe until both delete it

Edit:
Just as additional thought for that solution:
You could set both to -1 instead of deleting it on the second user deletion so you got a kind of archive and delete too old ones by a cronjob.

Upvotes: 3

Mahdi
Mahdi

Reputation: 9417

if you want sender see the message even after it's deleted by the receiver, you can use the following logic:

  1. Add a new column to your table named deleted, where values 1 means the message has been deleted, and 0 means it's not deleted yet.
  2. If this is the receiver try to see their messages: WHERE deleted = 0
  3. If this is the sender simply show all; don't use the above WHERE clause.
  4. for removing the message, in your PHP delete function first check the value of deleted, if it is 1 you can permanently delete the row from your table using SQL DELETE. Otherwise, you just need to update the value of deleted column to 1

Upvotes: 1

Related Questions