/
gestionarTabla.php
116 lines (100 loc) · 3.03 KB
/
gestionarTabla.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Obtener la base de datos y tabla que estamos gestionando
*
* Obtener todas sus tuplas
*
* Mostrarlo en una tabla
*
* Cada fila con un submit de editar o de borrar
*
* También dos opciones de añadir fila o cancelar
*/
spl_autoload_register(function ($clase) {
require_once "$clase.php";
});
session_start();
//Este scrip tiene posibles acciones (tiene varios submit que he de gestionar=
//Todo esto has de rellenarlo y ver qué información necesitas
//Puede que tengas que pasar valores por la url, tomarlos del formulario, sesiones ...
if (isset($_POST['submit'])) {
switch ($_POST['submit']) {
case 'Add':
header("Location:insertar.php");
break;
case 'Edit':
header("Location:editar.php");
exit();
break;
case 'Del':
//Quiero borrar el registro soleccionado y quedarme aquí
$msj="Se habría borrado la fila o error";
break;
case 'Close':
//Necesitamos pasar la base de datos
header("Location:tablas.php");
break;
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Ejemplo de estilos CSS en un archivo externo</title>
<link rel="stylesheet" type="text/css" href="estilo.css" media="screen"/>
<meta charset="ISO-8859-1"
<title></title>
</head>
<body>
<fieldset style="width:70%">
<legend>Admnistración de la tabla <?php echo $tabla; ?></legend>
<?= $msj ?? null?>
<table id="tabla" class="display" border="1">
<thead>
<tr>
<th>Campo 1</th>
<th>Campo n</th>
<th colspan="2">Acciones</th>
</tr>
</thead>
<tbody>
<tr>
<form action='gestionarTabla.php' method='post'>
<td>valor campo 1</td>
<td>valor campo 2</td>
<td><input id=tabla type="submit" value="Edit" name="submit"></td>
<td><input id=tabla type="submit" value="Del" name="submit"></td>
</form>
</tr>
</table>
<form action='gestionarTabla.php' method='post'>
<input type="submit" value="Add" name="submit">
<input type="submit" value="Close" name="submit">
</form>
</fieldset>
</body>
</html>
<?php
/**
* función usada para borrar
* Aquí puedo leer el input registros que contendrá todos los campos
*/
function borrar($tabla, $database, $datosConexion)
{
//He de leer todos los campos de esa fila que quiero borrar (nombre de campo y valor)
$registro = $_POST['campos'];
$sentencia = "delete from $tabla where ";
$condicion = "";
foreach ($registro as $campo => $valor) {
$condicion .= " ($campo = '$valor') and ";
}
$condicion = substr($condicion, 0, strlen($condicion) - 4);
$sentencia = "delete from $tabla where $condicion";
$db = new BD($datosConexion);
$db->usar_BD("$database");
$db->borrar($sentencia);
echo $db->get_error_message();
$db->cerrarDB();
}
?>