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";
I think it might be the best option to use multiple table -- one for each user -- in order to archive that. If you use only one table, then overtime it will become really big.
The solution I propose is that you edit your table structure into:
This way, when a user create a message, two records will be created: the sender's copy, and the recivier's copy
When UserA send UserB message "Good Job", the query will be:
sendmessage.php
inbox.php
outbox.php
delete.php
just delete the one that the owner='$you' DELETE FROM mbox WHERE condition1=value1 AND owner='$you'
Basically, my workaround is like: when a user send message, then we insert two message to the database (one copy for the recipient's inbox, and the other copy for the sender's outbox)
When a user deleted his/her message, it will not be deleted from the other's inbox/outbox because each user have their own copy of the message
If you don't want to loose any data from your database and don't want to alter your database but still want your users to have the ability to "delete", their messages, just add a string in front of "sentby" or "sentto" field. This applies if sentBy and sentTo have string values
In your "delete" action if this is done by UserB add a string "DeleteD---" in front of UserB in the db field sentto. So now your db gets like this:
In your php code make some functions that would check if the string "DeleteD---" exists in the sentBy or sentTo fields before doing anything else:
This way you "keep" the info in your database table to be available for both users making sure that if UserB delete the message sent by UserA, then UserA will still be able to see in his outbox the user that he sent the massage to. If both users do delete the same message sometime later on then you can completely delete from the database.
In case sentBy and sentTo have userid values (integers), then i propose adding just one field in the end of the table
how the above field works? As a binary indicator for sentby and sentto regarding who has deleted the message from his mailbox. If message is not deleted then value is 0 if it is deleted then value is 1. First bit is for sentBy and second bit is for sentTo. This way you store less data and create only one field in your current table. How you analyse the 2 bits?
I think you can keep your current table structure for the message content. Rather than adding on separate columns or deleted flags, you'd be better off having a separate table for mailboxes.
So your current mbox table:
Then another table for user_mailboxes
You'd have to do three total inserts when writing a message, one to the message table, on for each user in the user_mailboxes table.
So your mbox data looks like this:
And user_mailboxes data would look like this:
This allows you to delete individual rows for the user_mailboxes table. This would also allow for future add-ons by allowing you to send messages to multiple users at the same time (A new row for each user), and allow you to add more than one mailbox if needed (In, Out, Trash, Important, etc).
To look up the mail for a user for a particular mailbox, you'd just use a join
You'd need a clean up script as you delete to make sure there are no orphaned messages that do not exist in the user_mailboxes table.
This may not be the most robust solution, but it is a fairly functional one, and doesn't require you to make any changes to your DB structure.
Change your delete function. Instead of deleting the row in the database, do a few checks. Figure out whether or not it is the sender or the recipient who is doing the deleting. If the sender is deleting, check if
sentto == null
. If it is, Delete the row. Else, setsentby = null
. And vice versa.I'll be assuming you post the message ID when the user presses delete. Also assuming you are using PDO. Let me know if that assumption is wrong.
delete.php
To Your table I added 2 fields,both will be having YES and NO as values.At first both the fields will be
NO
When user deletes data from inbox you will be actually updating the
deteled_from_inbox
withYES
,So It wont show in inbox part.As we are not touching thedeteled_from_outbox
it will be showing in the outbox side.