Reputation: 47
I'm simply trying to implement an undo button using PHP and MySQL, for example if a user deletes a post or any general item there will be an option to undo that MySQL query and leave the post as it was. Thanks in advance.
Upvotes: 0
Views: 3435
Reputation: 428
For clarification, once a row is deleted from a MySQL database, it can't be "undone". There's no going back once an item has been deleted.
So, to mimic this "undo" functionality, you need to (as already suggested in another answer) create a new field in the database table that keeps track of whether a post has been deleted or not. Let's call this new field Deleted
and say it can have only two values -- yes
(meaning it has been deleted) and no
(meaning it hasn't been deleted).
When a post is deleted, you can mark that Deleted
field as yes
and "hide" the post from view so that it appears to have been deleted (even though it actually is still present in the database).
Likewise, when a user click on the "undo" button that will switch the Deleted
field back to no
.
In your PHP code, you'll want to check the value of the Deleted
field for each post, and then hide or show the post accordingly.
As with any challenge like this, there are multiple solutions. If the above approach isn't what you're looking for, and you really want to actually delete the post from the database, then you could save the deleted post in another database table first (and keep a reference to it, of course, so that you can restore it if the "undo" button is used), and then delete the post from the "main" database.
Upvotes: 4
Reputation: 1077
When you delete the post just mark it as not 'live'. Then to undo just mark it as 'live' again.
Then in your queries just filter results based on if they are 'live' or not.
Upvotes: 0
Reputation: 160833
You need a flag column to do thing. For example, the del_flag
, if the user delete a record, set this flag to 1
, when undo, set it to 0
.
Upvotes: 6