-3

How can I implement a undo changes function in my php application, so if the user for example added a new member to a group, or deleted item a notification message will appear: The selected item have been deleted, undo delete action(link that trigger the undo function).

The following is a working code:

public function deleteRecord($id) {
            $group = $this->query('SET autocommit=0');
            $group = $this->query('DELETE FROM  `members` WHERE memberID = '.$id.'');

    }


    public function undo_delete() {
            $member = $this->query('rollback');
    if($member) {
            trigger_error('Undo action has been completed. ', E_USER_NOTICE);
            return true;
}

    }

The only problem with this code is the transaction has not been submitted, so rollback or log out will undo all the changes that has been done during the user session, I am not sure if this is the best approach to create a redo function, any suggestions?

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
Belal Almassri
  • 119
  • 1
  • 8
  • 2
    In my travels I haven't seen your requirement discussed very often. If you don't trust the DML logic coming from PHP, then review that logic until you do. I prefer undoing something by doing another action which cancels it out. – Tim Biegeleisen Sep 12 '17 at 05:59
  • It is advised not to execute delete query from your code as it may end up deleting crucial data also, if any run time error or unexpected situation occurs. I would suggest you add a column status in your table like 1 is valid entry and 0 is invalid or removed entry. Managing rollback would be easy in that case you will just have to change the value of the status column. – Vijay Rathore Sep 12 '17 at 06:05
  • Thank you @VijayRathore for your polite answer, I have this system implemented already in my application, the client requested undo update as well, and undo add, so basically and action can be rolledback. – Belal Almassri Sep 12 '17 at 06:09
  • used sql-transaction instead – Ramgy Borja Sep 12 '17 at 06:11
  • rolling back transactions needs the transaction not to be finished yet. however, when the php code reaches its end for one request, the transaction finishes and you cannot roll it back in another transaction (at least not with the database transactional roll back). as Tim Biegeleisen suggested, use another query that reverts the previous action, which can be very hard without a versioned approach. – Jakumi Sep 12 '17 at 06:11
  • Thanks @Jakumi, another query that reverts the previous action requires the updated, deleted data to be saved somewhere else in order to be reverted? – Belal Almassri Sep 12 '17 at 06:21
  • yes, that's what makes it hard (not even counting associated rows which get deleted as well) – Jakumi Sep 12 '17 at 06:23

2 Answers2

1

You will not be able to undo with 2 different requests .Because when you will call the undo . It will undo all delete happened for all the members . To solve it I would suggest -

  1. You create a new table where you keep the information of the deleted record, eg- deleted_members.
  2. When user calls for undo fetch the data from the deleted_members table and insert into original members table.
  3. After successful insert in original table ,remove the data from the delete information table.

I hope this helped.

Vijay Rathore
  • 593
  • 8
  • 16
  • This is a very good answer, I will wait for more solutions and then I will decide which approach to consider. Thank you very much. – Belal Almassri Sep 12 '17 at 06:30
  • Point number 3 is a bit tricky as I am unsure when to delete the record from the information table, I want ideally if the user didn't click restore or undo button, then the data will be removed. – Belal Almassri Sep 12 '17 at 11:16
  • If you give me the solution for number 3 then this will accept your answer as the correct one. – Belal Almassri Sep 12 '17 at 11:17
  • You can have the same structure for deleted_members as members table. Make the member_id as unique key . Select the data from deleted_members table based on the member_id and insert it back in members table. Once insert in members table is successful , run delete on deleted_members table for that member_id. This will even help you to track the information of deleted members. – Vijay Rathore Sep 12 '17 at 14:11
0

Transactions do not survive across PHP requests. If a transaction is still open at the end of the first request, it will be implicitly rolled back when the MySQL connection closes at the end of the request. As a result, there is no longer a transaction to roll back when an undo is requested later.

If you want to implement this functionality, it will need to be implemented in application logic.