0
Is there any way to recover deleted data from Database? I’m making an application where I have the buttons to add, edit and delete and now I’m thinking about doing another to undo a delete. How can I do that? I believe there are several ways, could I quote some?
<?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'; ?>
How so standard of 0? And in the "id = 1" that id would be the fruit?
– Marcelo T. Cortes
The
idwould be theidyou wish to delete, in caseid = '$delete_id'for example, normally. The default ofDeletadoshould be, in this case,0. In this way all elements created in the future (and already created) will be0. For exampleALTER TABLE tabela ADD COLUMN Deletado TINYINT(1) DEFAULT 0;, this way will create a column in thetabelabeingtinyintwith standard of0. If you are using Phpmyadmin or Heidisql, they allow you to create a default value, then just create a columnDeletadowith the standard0, beingINTorTINYINT.– Inkeliz