I created a simple messaging system on my website where new registered users can send message to one another. the following mysql
statement works well on my site,but
my problem is- when UserA
sends a message to UserB
, The message is shown to UserB
in his Inbox, And The message is shown to UserA
in his Outbox, now if for some reasons UserB
deleted the message from his Inbox, then the message is deleted from both sides, I am storing all message in 1 table, now what I want to achieve is when the message is deleted from inbox it should still remain in Outbox, any help is much appreciated! thanks!
Table structure is as follows
id message sentby sentto created
Inbox.php
$you=$_COOKIE['username'];
$st= "SELECT* FROM mbox WHERE sentto='$you' ORDER BY ID DESC LIMIT 10";
outbox.php
$you=$_COOKIE['username'];
$st= "SELECT*FROM mbox WHERE sentby='$you' ORDER BY ID DESC LIMIT 10";
Instead of deleting messages from database, use status of that particular message
As SHOWtoSender,SHOWtoReciver,SHOWtoBoth or SHOWtoNONE
(use data type ENUM and default as SHOWtoBoth).
Make changes in your table as:
id sender receiver status time
Add a column like
has_mail
which have default value likeAB
which means both the users have the mail. Now if anyone deleted from their in/out box then particular A/B will be removed.Now you can delete the message from db when both fields are empty:
With your current database structure, no, you cannot do that. Let start by making change to some of your structure to achieve what you want.
1. Add deleted field
The first thing to do is to add one deleted field where it is ENUM(Y, N). The structure of your table will look like this.
Now, with deleted field, it allows the receiver to delete their message and the sender still keep their message. The problem is that it does not allow the sender to delete their message from their Outbox.
Outbox
Inbox
2. Make two tables
In order to make it more flexible (1) so that sender can delete the message from their outbox, but receive still can retain the message in their inbox (2) so that receiver can delete the message from their inbox, and sender still got it in their outbox.
Just do one thing add two new fields in your existing table
If someone delete it from outbox then make is_sender_deleted value to 1. So when you show data in outbox you just list all the records whose having is_sender_deleted field value 0.
Same situation ff someone delete it from inbox then make is_receiver_deleted value 1. So when show data in inbox you just list all the records whose having is_receiver_deleted value is 0.
Hope this solution helps you out.
You can add a column like "status" into mbox table,
Then; if UserB delete the message you can change status as 1 or UserA delete the message you can change status as 2.
For inbox :
For outbox :
Good luck.
I also solved this task. I think one table it is not useful in this case. So, i suggest use 2 tables:
I think it can fix all of your issues, because message users separated from each other
So, for one message we must create 3 inserts like this:
In this case for every user we create separated row in table
message_user
. So, when user_1 delete message in this inbox folder we mark it as 'deleted' and has no effect on the second user.So, that get all user messages we must run only simple SELECT like this: