Index.php
<h1>Mantenimiento actores de Sakila</h1> <form> <input type="hidden" name="action" value="insert"> Nombre:<input type="text" name="first_name"> Apellidos:<input type="text" name="last_name"> <input type="submit" class="btn btn-info"> </form> <table class="table"> <tr><td>Id</td><td>Nombre</td><td>Apellidos</td><td>Acciones</td></tr> <?php $server = "localhost"; $user = "root"; $password = ""; $db = "sakila"; $action = filter_input(INPUT_GET, 'action'); $last_name = filter_input(INPUT_GET, 'last_name', FILTER_SANITIZE_STRING); $first_name = filter_input(INPUT_GET, 'first_name', FILTER_SANITIZE_STRING); $actor_id = filter_input(INPUT_GET, 'actor_id', FILTER_VALIDATE_INT); try { $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Acciones sobre la base de datos if ($action == 'insert' && !empty($first_name) && !empty($last_name)) { $sql = "insert into actor (first_name, last_name) values ('$first_name','$last_name')"; $conn->exec($sql); } if ($action == "delete" && !empty($actor_id)) { $sql = "delete from actor where actor_id=$actor_id"; $conn->exec($sql); } if ($action == 'update' && !empty($first_name) && !empty($last_name) && !empty($actor_id)) { $sql = "update actor set first_name='$first_name', last_name='$last_name' where actor_id=$actor_id"; $conn->exec($sql); } $sql = "select * from actor"; $resul = $conn->query($sql); while ($fila = $resul->fetch(PDO::FETCH_ASSOC)) { ?> <tr><td><?= $fila['actor_id'] ?></td> <td><?= $fila['first_name'] ?></td> <td><?= $fila['last_name'] ?></td> <td><a href="?action=delete&actor_id=<?= $fila['actor_id'] ?>">Borrar</a> <a href="update.php?actor_id=<?= $fila['actor_id'] ?>">Editar</a></td></tr> <?php } } catch (Exception $ex) { echo "Ha ocurrido un error<br/>" . $ex->getMessage(); } ?></table>
update.php
<h1>Editar actor</h1> <?php $server = "localhost"; $user = "root"; $password = ""; $db = "sakila"; $actor_id = filter_input(INPUT_GET, 'actor_id', FILTER_VALIDATE_INT); if (!empty($actor_id)) { try { $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "select * from actor where actor_id=$actor_id"; $resul=$conn->query($sql); $fila=$resul->fetch(); } catch (Exception $ex) { echo "Ha ocurrido un error<br/>" . $ex->getMessage(); } } ?> <form action="index.php"> <input type="hidden" name="action" value="update"> <input type="hidden" name="actor_id" value="<?=$actor_id?>"> Nombre:<input type="text" name="first_name" value="<?=$fila['first_name']?>"> Apellidos:<input type="text" name="last_name" value="<?=$fila['last_name']?>"> <input type="submit" class="btn btn-info"> </form>
final.php
<h1>Consulta actores de Sakila</h1> <form> Nombre:<input type="text" name="first_name"> Apellidos:<input type="text" name="last_name"> <input type="submit" class="btn btn-info" value="Buscar"> </form> <table class="table"> <tr><td>Id</td><td>Nombre</td><td>Apellidos</td></tr> <?php $server = "localhost"; $user = "root"; $password = ""; $db = "sakila"; $last_name = filter_input(INPUT_GET, 'last_name', FILTER_SANITIZE_STRING); $first_name = filter_input(INPUT_GET, 'first_name', FILTER_SANITIZE_STRING); try { $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "select * from actor where 1"; if (!empty($first_name)){ $sql.=" and first_name like '%$first_name%'"; } if (!empty($last_name)){ $sql.=" and last_name like '%$last_name%'"; } $resul = $conn->query($sql); while ($fila = $resul->fetch(PDO::FETCH_ASSOC)) { ?> <tr><td><?= $fila['actor_id'] ?></td> <td><?= $fila['first_name'] ?></td> <td><?= $fila['last_name'] ?></td> </tr> <?php } } catch (Exception $ex) { echo "Ha ocurrido un error<br/>" . $ex->getMessage(); } ?></table>