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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
Deletedand say it can have only two values —yes(meaning it has been deleted) andno(meaning it hasn’t been deleted).When a post is deleted, you can mark that
Deletedfield asyesand “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
Deletedfield back tono.In your PHP code, you’ll want to check the value of the
Deletedfield 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.