How to make a rollback button or "undo" in my application?

0

Is there any way to recover the deleted data from the Database? I am making an application where I have the buttons to add, edit and delete and now I am thinking of doing another one to undo a delete. How can I do this? I believe there are several ways, could you name a few?

<?php 

require_once '../core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
//Get Brands database
$sql = "SELECT * FROM frutas ORDER BY frutas";
$results = $db->query($sql);
$errors = array();

//Editar
if(isset($_GET['edit']) && !empty($_GET['edit'])){
    $edit_id = (int)$_GET['edit'];
    $edit_id = sanitize($edit_id);
    $sql2 = "SELECT * FROM frutas WHERE id = '$edit_id'";
    $edit_result = $db->query($sql2);
    $eBrand = mysqli_fetch_assoc($edit_result);

}

//Deletar
if(isset($_GET['delete']) && !empty($_GET['delete'])){
    $delete_id = (int)$_GET['delete'];
    $delete_id = sanitize($delete_id);
    $sql = "DELETE FROM frutas WHERE id = '$delete_id'";
    $db->query($sql);
    header('Location: frutas.php');

}
//Se 'adicionar' for submetido
if(isset($_POST['add_submit'])){
    $frutas = sanitize($_POST['frutas']);
    //Checkar espaços em branco
    if($_POST['frutas'] == ''){
        $errors[] .= 'Adiciona alguma fruta!';
    }
    // Checar se alguma fruta existe no banco de dados
    $sql = "SELECT * FROM frutas WHERE frutas = '$frutas'";
    $result = $db->query($sql);
    $count = mysqli_num_rows($result);
    if($count > 0){
        $errors[] .= $frutas.' Já existe. Adicione outra.';
    }
    //Mostrar erros
    if (!empty($errors)) {
        echo display_errors($errors);
    }else{
        //Adicionar frutas no banco de dados
        $sql = "INSERT INTO frutas (frutas) VALUES ('$frutas')";
        $db->query($sql);
        header('Location: frutas.php');
    }
}

?>

<h2 class="text-center">PHP na feira da Fruta</h2><hr>
<!--Fruta form-->
<div class="text-center">
 <form class="form-inline" action="frutas.php" method="post">
    <div class="form-group">
        <label for="frutas">Adicionar Fruta:</label>
        <input type="text" name="frutas" id="frutas" class="form-control" value="<?=((isset($_POST['frutas']))?$_POST['frutas']:''); ?>">
        <input type="submit" name="add_submit" value="Adicionar" class="btn btn-success">
    </div>
 </form>    
</div><hr>

<table class="table table-bordered table-striped table-auto table-condensed" style="margin: 0 auto; width: auto;">
    <thead>
        <th></th><th>Frutas</th><th></th>
    </thead>
    <tbody>
    <?php while($frutas = mysqli_fetch_assoc($results)): ?>
        <tr>
            <td><a href="frutas.php?edit=<?=$frutas['id']; ?>" class="btn btn-sc btn-default"><span class="glyphicon glyphicon-pencil"></span></a></td>
            <td><?=$frutas['frutas'] ?></td>
            <td><a href="frutas.php?delete=<?=$frutas['id']; ?>" class="btn btn-sc btn-default"><span class="glyphicon glyphicon-remove-sign"></span></a></td>
        </tr>
    <?php endwhile?>
    </tbody>
</table>
</br>
<center><input type="submit" name="add_submit" value="DESFAZER DELETE" class="btn btn-success"></center>
<?php include 'includes/footer.php'; ?>

    
asked by anonymous 11.02.2017 / 20:40

1 answer

5

DELETE can not be undone directly unless you are in a TRANSACTION , in this case you could use ROLLBACK (using mysqli_rollback($conexao) ) ;

However this is not what you want , you want to delete it and then the user can restore the data in a new request.

To do this, the best thing to do is to use UPDATE , create a column of Deletado with the 0 pattern and then use it to identify whether or not an item is deleted.

/ p>

mysqli_query($conexao, 'UPDATE SET Deletado = 1 WHERE id = 1');

Then to "recover" use:

mysqli_query($conexao, 'UPDATE SET Deletado = 0 WHERE id = 1');

In this way the item will be "recovered" since your application will always use Deletado as the base. For this reason you should use SELECT * FROM tabela WHERE Deletado = 0 to know that the item is not deleted.

You can also create a DataDeletado to know the date the item was deleted, so you can create a "cronjob" (or the

11.02.2017 / 21:14