0

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.

Disorganizing
  • 47
  • 1
  • 6
  • Yeah...no one here is going to write code for you. What have you tried? – Brendan Lesniak Jun 07 '12 at 03:16
  • Sorry about that I'm really unsure of where to start – Disorganizing Jun 07 '12 at 03:17
  • It's great that you're trying things. But what's your question? – ghoti Jun 07 '12 at 03:19
  • Please be more specific here. xdazz has the correct approach if you're just trying to be able to undo a delete on the mysql side. If you're looking to be able to also undo `UPDATE` clauses, you'll need something a little different. Explain what you application currently does and what you'd like users to be able to do exactly. – Ben D Jun 07 '12 at 03:34

3 Answers3

6

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
4

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.

Jonathan Head
  • 428
  • 3
  • 7
0

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.

dibs
  • 1,018
  • 2
  • 23
  • 35