Gracias por tu respuesta, rbczgz, no sabía si tenía que añadir el código.
Añado el código del
index.php que es donde estoy tratando todo esto.
Código PHP:
<?php
require_once 'conexion.php';
// Listar
if(isset($_GET['opt']) and $_GET['opt'] == 'listar'){
$sql = "SELECT datos.id_datos,datos.nombre,datos.apellido,datos.deporte,datos.comentario,generos.id_generos,generos.generos,nexo.id_datos,nexo.id_generos
FROM datos,generos,nexo
WHERE datos.id_datos LIKE nexo.id_datos
AND nexo.id_generos LIKE generos.id_generos
ORDER BY datos.id_datos ASC";
$resul = mysqli_query($conexion,$sql);
if(!$resul){
$error = "Error en la consulta - ".mysqli_error($conexion);
include 'error.php';
exit();
}
$listar = array();
while ($fila = mysqli_fetch_array($resul)) {
$listar[] = $fila;
}
/* FALTA LA RUPTURA DE CONTROL */
include 'vista_listar.php';
exit();
}
// Buscar
if(isset($_POST['buscar']) and $_POST['buscar'] == 'Buscar'){
$texto = trim($_POST['texto']);
if(empty($texto)){
$error = 'Introduzca un texto a buscar';
include 'form_buscar.php';
exit();
}
$texto = mysqli_real_escape_string($conexion, $texto);
$sql = "SELECT nombre, apellido FROM datos
WHERE nombre LIKE '%$texto%' OR apellido LIKE '%$texto%'";
$resul = mysqli_query($conexion, $sql);
if (!$resul){
$error = "Error en consulta - ".mysqli_error($conexion);
include "error.php";
exit();
}
$personas = array();
while ($fila = mysqli_fetch_array($resul)){
$personas[] = $fila;
}
if (count($personas) == 0){
$error = "No hubo resultados en la búsqueda";
}
include "form_buscar.php";
exit();
}
if(isset($_GET['opt']) and $_GET['opt'] == 'buscar'){
include 'form_buscar.php';
exit();
}
// Recargar tras borrar
if (isset($_GET['oper']) && $_GET['oper'] == 'borrar'){
$mensaje = 'Registro borrado correctamente';
}
// Borrar (boton, tiene borrado en cascada)
if (isset($_POST['borrar']) && $_POST['borrar'] == 'Borrar'){
$id = mysqli_real_escape_string($conexion, $_POST['id']);
$sql = "DELETE FROM datos WHERE id_datos = '$id'";
$resul = mysqli_query($conexion, $sql);
if (!$resul){
$error = "Error en consulta - ".mysqli_error($conexion);
include "error.php";
exit();
}
header('Location: ?oper=borrar');
exit();
}
// Borrar (menu)
if(isset($_GET['opt']) and $_GET['opt'] == 'borrar'){
$sql = "SELECT * FROM datos";
$resul = mysqli_query($conexion,$sql);
if(!$resul){
$error = "Error en la consulta - ".mysqli_error($conexion);
include 'error.php';
exit();
}
$paraBorrar = array();
while($fila = mysqli_fetch_array($resul)){
$paraBorrar[] = $fila;
}
include 'form_borrar.php';
exit();
}
/* En este if, se comprueba que, si el botón "enviar" no ha sido pulsado, muestre el formulario. */
if(!isset($_POST['enviar'])){
include 'formulario.php';
exit();
}else{
include 'validaciones.php';
}
include 'formulario.php';
?>
Y este el código del
vista_listar.php Código PHP:
<?php
include 'cabeza.php';
echo "<table class=\"table table-hover\">
<tr>
<th>Nombre</th>
<th>Apellido</th>
<th>Deporte</th>
<th>Comentario</th>
<th>Generos Musicales</th>
</tr>";
foreach ($listar as $key) {
echo "<tr>";
echo "<td>".htmlspecialchars($key['nombre'])."</td>";
echo "<td>".htmlspecialchars($key['apellido'])."</td>";
echo "<td>".htmlspecialchars($key['deporte'])."</td>";
echo "<td>".htmlspecialchars($key['comentario'])."</td>";
echo "<td>".htmlspecialchars($key['generos'])."</td>";
echo "<tr>";
}
echo "</table>";
include 'pie.php';
?>
Es al principio de todo donde pone "// Listar".
Si queréis verlo en "acción" podeis verlo aquí -> http://mirabalphoto.es/iaw