| @ -0,0 +1,39 @@ | |||||
| <?php | |||||
| session_start(); | |||||
| if (empty($_SESSION['csrf'])) { | |||||
| if (function_exists('random_bytes')) { | |||||
| $_SESSION['csrf'] = bin2hex(random_bytes(32)); | |||||
| } else if (function_exists('mcrypt_create_iv')) { | |||||
| $_SESSION['csrf'] = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); | |||||
| } else { | |||||
| $_SESSION['csrf'] = bin2hex(openssl_random_pseudo_bytes(32)); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Escapes HTML for output | |||||
| * | |||||
| */ | |||||
| function escape($html) { | |||||
| return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8"); | |||||
| } | |||||
| /** | |||||
| * Función para escribir logs | |||||
| */ | |||||
| function escribe_log($log_msg) | |||||
| { | |||||
| $log_dir = $_SERVER['DOCUMENT_ROOT'] ."/gestionrepostajes/log"; | |||||
| if (!file_exists($log_dir)) | |||||
| { | |||||
| // create directory/folder uploads. | |||||
| mkdir($log_dir, 0777, true); | |||||
| } | |||||
| $log_file_data = $log_dir.'/log_' . date('Y-M-d') . '.log'; | |||||
| // if you don't add `FILE_APPEND`, the file will be erased each time you add a log | |||||
| file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND); | |||||
| } | |||||
| @ -0,0 +1,15 @@ | |||||
| <?php | |||||
| /** | |||||
| * Configuration for database connection | |||||
| * | |||||
| */ | |||||
| $host = "repostajes-mysql"; | |||||
| $username = "root"; | |||||
| $password = "Dsa-0213"; | |||||
| $dbname = "automoviles"; | |||||
| $dsn = "mysql:host=$host;dbname=$dbname"; | |||||
| $options = array( | |||||
| PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION | |||||
| ); | |||||
| @ -0,0 +1,36 @@ | |||||
| CREATE DATABASE automoviles; | |||||
| USE automoviles; | |||||
| DROP TABLE IF EXISTS carburante; | |||||
| CREATE TABLE carburante ( | |||||
| fecha date DEFAULT NULL, | |||||
| vehiculo int(11) DEFAULT NULL, | |||||
| kms int(11) DEFAULT NULL, | |||||
| litros decimal(10,3) DEFAULT NULL, | |||||
| descuento decimal(10,2) DEFAULT NULL, | |||||
| precioxlitro decimal(10,3) DEFAULT NULL, | |||||
| importe decimal(10,2) DEFAULT NULL | |||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish_ci; | |||||
| DROP TABLE IF EXISTS vehiculos; | |||||
| CREATE TABLE vehiculos ( | |||||
| identificador int(11) NOT NULL, | |||||
| marca text NOT NULL, | |||||
| modelo text NOT NULL, | |||||
| matricula text NOT NULL | |||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; | |||||
| ALTER TABLE vehiculos | |||||
| ADD PRIMARY KEY (identificador); | |||||
| ALTER TABLE vehiculos | |||||
| MODIFY identificador int(11) NOT NULL AUTO_INCREMENT; | |||||
| COMMIT; | |||||
| @ -0,0 +1,13 @@ | |||||
| CREATE DATABASE test; | |||||
| use test; | |||||
| CREATE TABLE users ( | |||||
| id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |||||
| firstname VARCHAR(30) NOT NULL, | |||||
| lastname VARCHAR(30) NOT NULL, | |||||
| email VARCHAR(50) NOT NULL, | |||||
| age INT(3), | |||||
| location VARCHAR(50), | |||||
| date TIMESTAMP | |||||
| ); | |||||
| @ -0,0 +1,16 @@ | |||||
| <?php include "templates/header.php"; ?> | |||||
| <section id="one" class="main style1"> | |||||
| <div class="inner"> | |||||
| <ul class="actions special"> | |||||
| <li><a href="vehiculos/menu.php"><strong>Gestión de vehículos</strong></a></li> | |||||
| <li><a href="repostajes/menu.php"><strong>Control de respostajes</strong></a></li> | |||||
| </ul> | |||||
| </div> | |||||
| </section> | |||||
| <br> | |||||
| <!-- <a href="../install.php"><strong>Crear base de datos</strong></a> --> | |||||
| <?php include "templates/footer.php"; ?> | |||||
| @ -0,0 +1,96 @@ | |||||
| <?php | |||||
| /** | |||||
| * Use an HTML form to create a new entry in the | |||||
| * users table. | |||||
| * | |||||
| */ | |||||
| require './config.php'; | |||||
| require './common.php'; | |||||
| /** | |||||
| * Esto es para comprobar si existe la BD | |||||
| */ | |||||
| try { | |||||
| $conexion = new PDO( $dsn, $username, $password, $options ); | |||||
| include './templates/header.php'; | |||||
| $sql = "SELECT * | |||||
| FROM carburante | |||||
| ORDER BY fecha DESC LIMIT 10" ; | |||||
| $statement = $conexion->prepare($sql); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Últimos repostajes</h2> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Fecha</th> | |||||
| <th>Vehículo</th> | |||||
| <th>Kilómetros</th> | |||||
| <th>Litros</th> | |||||
| <th>Descuento</th> | |||||
| <th>Precio/litro</th> | |||||
| <th>Importe</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php foreach ($result as $row) : ?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["fecha"]); ?></td> | |||||
| <td><?php echo escape($row["vehiculo"]); ?></td> | |||||
| <td><?php echo escape($row["kms"]); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["litros"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["descuento"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["precioxlitro"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["importe"])); ?></td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <!-- <a href = '../install.php'><strong>Crear base de datos</strong></a> --> | |||||
| <?php include './templates/footer.php'; | |||||
| } catch( PDOException $error ) { | |||||
| require './templates/header.php'; | |||||
| ?> | |||||
| <section id='four' class='main style2 special'> | |||||
| <div class='container'> | |||||
| <header class='major'> | |||||
| <h2>Base de datos inexistente</h2> | |||||
| </header> | |||||
| <section> | |||||
| <p>La base de datos no existe. ¿quieres inicializarla?</p> | |||||
| <ul class='actions special'> | |||||
| <li><a href='/install.php'><strong>Crear base de datos</strong></a></li> | |||||
| </ul> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require './templates/footer.php'; | |||||
| } | |||||
| ?> | |||||
| @ -0,0 +1,38 @@ | |||||
| <?php | |||||
| /** | |||||
| * Open a connection via PDO to create a | |||||
| * new database and table with structure. | |||||
| * | |||||
| */ | |||||
| require "config.php"; | |||||
| try { | |||||
| $connection = new PDO("mysql:host=$host", $username, $password, $options); | |||||
| $sql = file_get_contents("data/automoviles.sql"); | |||||
| $connection->exec($sql); | |||||
| require "./gestionrepostajes/templates/header.php"; ?> | |||||
| <section id="four" class="main style2 special"> | |||||
| <div class="container"> | |||||
| <header class="major"> | |||||
| <h2>Base de datos creada</h2> | |||||
| </header> | |||||
| <section> | |||||
| <p>La base de datos ha sido creada con éxito</p> | |||||
| <ul class="actions special"> | |||||
| <li><a href="./gestionrepostajes/vehiculos/create.php"><strong>Crear vehículos</strong></a></li> | |||||
| </ul> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "./gestionrepostajes/templates/footer.php"; | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| @ -0,0 +1,9 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 63.88. Total introducido: 63,88 | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 63.88. Total introducido: 63,88 | |||||
| BORRA REPOSTAJE-> ID: 194 | |||||
| BORRA REPOSTAJE-> ID: 195 | |||||
| BORRA REPOSTAJE-> ID: 196 | |||||
| BORRA REPOSTAJE-> ID: 197 | |||||
| BORRA REPOSTAJE-> ID: 198 | |||||
| BORRA REPOSTAJE-> ID: 199 | |||||
| BORRA REPOSTAJE-> ID: 200 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.3899. Total con descuento: 44.9401. Total introducido: 46,33 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.2186. Total con descuento: 39.4014. Total introducido: 40,62 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 77.06. Total introducido: 77,06 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 57.62. Total introducido: 57,62. Kilometros anteriores: 87503. Kilometros recorridos: 753 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.6632. Total con descuento: 53.7768. Total introducido: 55,44. Kilometros anteriores: 88256. Kilometros recorridos: 708 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.1685. Total con descuento: 37.7815. Total introducido: 38,95. Kilometros anteriores: 12006. Kilometros recorridos: 421 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.7112. Total con descuento: 55.3288. Total introducido: 57,04. Kilometros anteriores: 86021. Kilometros recorridos: 736 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 40. Total introducido: 40. Kilometros anteriores: 12427. Kilometros recorridos: 506 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.746. Total con descuento: 56.454. Total introducido: 58,2. Kilometros anteriores: 86757. Kilometros recorridos: 746 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 72.43. Total introducido: 72,43 | |||||
| @ -0,0 +1,8 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 89. Total introducido: 89. Kilometros anteriores: Array | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 78. Total introducido: 78. Kilometros anteriores: 90000 | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 78. Total introducido: 78. Kilometros anteriores: 91000. Kilometros recorridos: 1000 | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 66. Total introducido: 66. Kilometros anteriores: 11508. Kilometros recorridos: 8492 | |||||
| BORRA REPOSTAJE-> ID: 210 | |||||
| BORRA REPOSTAJE-> ID: 209 | |||||
| BORRA REPOSTAJE-> ID: 211 | |||||
| BORRA REPOSTAJE-> ID: 208 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.2486. Total con descuento: 40.3714. Total introducido: 41,62. Kilometros anteriores: 11508. Kilometros recorridos: 498 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 70.11. Total introducido: 70,11. Kilometros anteriores: 85104. Kilometros recorridos: 917 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.1442. Total con descuento: 36.9958. Total introducido: 38,14 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 77.26. Total introducido: 77,26 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 35.85. Total introducido: 35,85 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.341. Total con descuento: 43.359. Total introducido: 44,7. Kilometros anteriores: 12933. Kilometros recorridos: 481 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 1.6098. Total con descuento: 52.0502. Total introducido: 53,66. Kilometros anteriores: 88964. Kilometros recorridos: 838 | |||||
| @ -0,0 +1,2 @@ | |||||
| BORRA REPOSTAJE-> ID: 221 | |||||
| CREA REPOSTAJE-> Descuento: 1.9098. Total con descuento: 61.7502. Total introducido: 63,66. Kilometros anteriores: 88964. Kilometros recorridos: 838 | |||||
| @ -0,0 +1 @@ | |||||
| CREA REPOSTAJE-> Descuento: 0. Total con descuento: 37.09. Total introducido: 37,09. Kilometros anteriores: 13414. Kilometros recorridos: 489 | |||||
| @ -0,0 +1,165 @@ | |||||
| <?php | |||||
| /** | |||||
| * Use an HTML form to create a new entry in the | |||||
| * users table. | |||||
| * | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| if (isset($_POST['submit'])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| if (empty($_POST['fecha']) or | |||||
| empty($_POST['kms']) or | |||||
| empty($_POST['litros']) or | |||||
| empty($_POST['importe'])) { | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <section id="four" class="main style2 special"> | |||||
| <div class="container"> | |||||
| <header class="major"> | |||||
| <h2>Introducción de repostajes</h2> | |||||
| </header> | |||||
| <section> | |||||
| <p>Por favor, rellena todos los campos.</p> | |||||
| <ul class="actions special"> | |||||
| <li><a href="create.php" class="button wide primary">Volver</a></li> | |||||
| </ul> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| <?php | |||||
| die(); | |||||
| } else { | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| // Calcula el precio por litro | |||||
| //$total = floatval($_POST['importe']); | |||||
| $total=floatval(str_replace(',', '.', str_replace('.', '', $_POST['importe']))); | |||||
| $cantidad = floatval(str_replace(',', '.', str_replace('.', '', $_POST['litros']))); | |||||
| if(empty($_POST['dtoing'])) { | |||||
| $dto = 0; | |||||
| } else { | |||||
| $dto = ($total*0.03); | |||||
| } | |||||
| //$dto = intval($_POST['descuento']); | |||||
| $totalcondto = $total-$dto; | |||||
| $ellitro = ($totalcondto / $cantidad); | |||||
| // Estas líneas son para conocer los kms del último repostaje | |||||
| $sql = sprintf('SELECT kms FROM carburante WHERE vehiculo = ? order by identificador DESC LIMIT 1'); | |||||
| $smt = $connection->prepare($sql); | |||||
| $smt->execute([$_POST['vehiculo']]); | |||||
| $filas = $smt->fetchAll(); | |||||
| foreach ($filas as $row) { | |||||
| $kmsanteriores=$row["kms"]; | |||||
| } | |||||
| $kmsrecorridos=$_POST['kms']-$kmsanteriores; | |||||
| // | |||||
| // Hasta aquí lo de conocer los kms del último repostaje | |||||
| $nuevo_repostaje = array( | |||||
| "fecha" => $_POST['fecha'], | |||||
| "vehiculo" => $_POST['vehiculo'], | |||||
| "kms" => $_POST['kms'], | |||||
| "litros" => str_replace(',', '.',$_POST['litros']), | |||||
| "descuento" => $dto, | |||||
| "precioxlitro" => str_replace(',', '.',strval($ellitro)), | |||||
| "importe" => $totalcondto, | |||||
| ); | |||||
| $sql = sprintf( | |||||
| "INSERT INTO %s (%s) values (%s)", | |||||
| "carburante", | |||||
| implode(", ", array_keys($nuevo_repostaje)), | |||||
| ":" . implode(", :", array_keys($nuevo_repostaje)) | |||||
| ); | |||||
| /* | |||||
| * Logs | |||||
| */ | |||||
| $log_msg='CREA REPOSTAJE-> Descuento: '.$dto.'. Total con descuento: '.$totalcondto.'. Total introducido: '.$_POST['importe'].'. Kilometros anteriores: '.$kmsanteriores.'. Kilometros recorridos: '.$kmsrecorridos; | |||||
| escribe_log($log_msg); | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute($nuevo_repostaje); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <?php if (isset($_POST['submit']) && $statement) : ?> | |||||
| <blockquote><?php echo escape($_POST['fecha']); ?> successfully added.</blockquote> | |||||
| <?php endif; ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Añadir repostaje</h2> | |||||
| <form method="post"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <p><label for="fecha">Fecha</label> | |||||
| <p><input type="date" name="fecha" id="fecha" required> | |||||
| <p><label for="vehiculo">Vehículo</label> | |||||
| <p><select name="vehiculo" id="vehiculo" required> | |||||
| <option value="" disabled selected>Selecciona un vehículo</option> | |||||
| <?php | |||||
| try { | |||||
| $conexion = new PDO($dsn, $username, $password, $options); | |||||
| $smt = $conexion->prepare('SELECT matricula FROM vehiculos'); | |||||
| $smt->execute(); | |||||
| $vehiculos = $smt->fetchAll(); | |||||
| foreach ($vehiculos as $row) { | |||||
| echo "<option value='".$row["matricula"]."'>" . $row["matricula"]."</option>"; | |||||
| } | |||||
| } catch (PDOException $e) { | |||||
| echo "Error: " . $e->getMessage(); | |||||
| } | |||||
| ?> | |||||
| </select> | |||||
| <p><label for="kms">Kilómetros</label></p> | |||||
| <p><input type="number" name="kms" id="kms" required></p> | |||||
| <p><label for="litros">Litros</label></p> | |||||
| <p><input type="number" name="litros" id="litros" required></p> | |||||
| <p><label for="importe">Importe</label></p> | |||||
| <p><input type="number" name="importe" id="importe" required></p> | |||||
| <p><input type="checkbox" id="dtoing" name="dtoing"> | |||||
| <label for="dtoing">Descuento ING</label> | |||||
| </p> | |||||
| <br> | |||||
| <p><input type="submit" name="submit" value="Crear" class="primary"></p> | |||||
| </form> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,98 @@ | |||||
| <?php | |||||
| /** | |||||
| * Delete a user | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| $success = null; | |||||
| if (isset($_POST["submit"])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $identificador = $_POST["submit"]; | |||||
| $sql = "DELETE FROM carburante WHERE identificador = :identificador"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->bindValue(':identificador', $identificador); | |||||
| $statement->execute(); | |||||
| $success = "Repostaje borrado con éxito"; | |||||
| $log_msg='BORRA REPOSTAJE-> ID: '.$identificador; | |||||
| escribe_log($log_msg); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $sql = "SELECT * FROM carburante ORDER BY fecha DESC"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Borrar repostajes</h2> | |||||
| <?php if ($success) echo $success; ?> | |||||
| <form method="post"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Fecha</th> | |||||
| <th>Vehículo</th> | |||||
| <th>Kilómetros</th> | |||||
| <th>Litros</th> | |||||
| <th>Descuento</th> | |||||
| <th>Precio/litro</th> | |||||
| <th>Importe</th> | |||||
| <th>Borrar</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php foreach ($result as $row) : ?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["fecha"]); ?></td> | |||||
| <td><?php echo escape($row["vehiculo"]); ?></td> | |||||
| <td><?php echo escape($row["kms"]); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["litros"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["descuento"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["precioxlitro"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["importe"])); ?></td> | |||||
| <td><button type="submit" name="submit" | |||||
| value="<?php echo escape($row["identificador"]); ?>">Borrar</button></td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </form> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,23 @@ | |||||
| <?php include "../templates/header.php"; ?> | |||||
| <section id="three" class="main style1 special"> | |||||
| <div class="container"> | |||||
| <header class="inner"> | |||||
| <h2>Menú repostajes</h2> | |||||
| <!-- <span class="icon solid major fa-gas-pump"></span> --> | |||||
| </header> | |||||
| <ul class="actions special"> | |||||
| <li><a href="create.php"><strong>Crear un repostaje</strong></a> </li> | |||||
| <li><a href="read.php"><strong>Mostrar repostajes</strong></a> </li> | |||||
| <li><a href="update.php"><strong>Actualizar repostajes</strong></a> </li> | |||||
| <li><a href="delete.php"><strong>Borrar repostajes</strong></a></li> | |||||
| </ul> | |||||
| </div> | |||||
| <div class="container"> | |||||
| <a href="../index.php" class="button small">Volver</a> | |||||
| </div> | |||||
| </section> | |||||
| <br> | |||||
| <?php include "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,118 @@ | |||||
| <?php | |||||
| /** | |||||
| * Function to query information based on | |||||
| * a parameter: in this case, marca. | |||||
| * | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| /** | |||||
| * Esto es para obtener los vehículos dados de alta | |||||
| */ | |||||
| $conexion = new PDO($dsn, $username, $password, $options); | |||||
| $smt = $conexion->prepare('SELECT matricula FROM vehiculos'); | |||||
| $smt->execute(); | |||||
| $vehiculos = $smt->fetchAll(); | |||||
| if (isset($_POST['submit'])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $sql = "SELECT * | |||||
| FROM carburante | |||||
| WHERE vehiculo = :vehiculo ORDER BY fecha ASC" ; | |||||
| $marca = $_POST['vehiculo']; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->bindParam(':vehiculo', $marca, PDO::PARAM_STR); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Busca repostajes por vehículo</h2> | |||||
| <form method="post"> | |||||
| <div class="row gtr-uniform gtr-50"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <div class="col-12"> | |||||
| <label for="vehiculo">Vehículo</label> | |||||
| <select name="vehiculo" id="vehiculo"> | |||||
| <?php foreach ($vehiculos as $row) { ?> | |||||
| <option><?=$row["matricula"]?></option> | |||||
| <?php } ?> | |||||
| </select> | |||||
| </div> | |||||
| <br> | |||||
| <div class="col-12"> | |||||
| <input type="submit" name="submit" value="Ver resultados" class="primary"> | |||||
| </div> | |||||
| </div> | |||||
| </form> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| <?php | |||||
| if (isset($_POST['submit'])) { | |||||
| if ($result && $statement->rowCount() > 0) { ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Resultados para vehículo <?php echo escape($_POST['vehiculo']); ?></h2> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Fecha</th> | |||||
| <th>Vehículo</th> | |||||
| <th>Kilómetros</th> | |||||
| <th>Litros</th> | |||||
| <th>Descuento</th> | |||||
| <th>Precio/litro</th> | |||||
| <th>Importe</th> | |||||
| <th>Recorridos</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php $kmsanterior=0; | |||||
| foreach ($result as $row) : | |||||
| $kmsrecorridos=$row["kms"]-$kmsanterior; | |||||
| if($kmsrecorridos==$row["kms"]) {$kmsrecorridos=0;} | |||||
| $kmsanterior = $row["kms"];?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["fecha"]); ?></td> | |||||
| <td><?php echo escape($row["vehiculo"]); ?></td> | |||||
| <td><?php echo escape($row["kms"]); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["litros"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["descuento"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["precioxlitro"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["importe"])); ?></td> | |||||
| <td><?php echo escape($kmsrecorridos); ?></td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php } else { ?> | |||||
| <blockquote>No results found for <?php echo escape($_POST['vehiculo']); ?>.</blockquote> | |||||
| <?php } | |||||
| } ?> | |||||
| @ -0,0 +1,90 @@ | |||||
| <?php | |||||
| /** | |||||
| * Use an HTML form to edit an entry in the | |||||
| * kmss table. | |||||
| * | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| if (isset($_POST['submit'])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $kms =[ | |||||
| "identificador" => $_POST['identificador'], | |||||
| "fecha" => $_POST['fecha'], | |||||
| "kms" => $_POST['kms'], | |||||
| "litros" => str_replace(',', '.',$_POST['litros']), | |||||
| "descuento" => str_replace(',', '.',$_POST['descuento']), | |||||
| "precioxlitro" => str_replace(',', '.',$_POST['precioxlitro']), | |||||
| "importe" => str_replace(',', '.',$_POST['importe']) | |||||
| ]; | |||||
| $sql = "UPDATE carburante | |||||
| SET identificador = :identificador, | |||||
| fecha = :fecha, | |||||
| kms = :kms, | |||||
| litros = :litros, | |||||
| descuento = :descuento, | |||||
| precioxlitro = :precioxlitro, | |||||
| importe = :importe | |||||
| WHERE identificador = :identificador"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute($kms); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| if (isset($_GET['identificador'])) { | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $identificador = $_GET['identificador']; | |||||
| $sql = "SELECT * FROM carburante WHERE identificador = :identificador"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->bindValue(':identificador', $identificador); | |||||
| $statement->execute(); | |||||
| $kms = $statement->fetch(PDO::FETCH_ASSOC); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } else { | |||||
| echo "¡Algo ha salido mal!"; | |||||
| exit; | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <?php if (isset($_POST['submit']) && $statement) : ?> | |||||
| <blockquote><?php echo escape($_POST['fecha']); ?> successfully updated.</blockquote> | |||||
| <?php endif; ?> | |||||
| <section id="five" class="main style1"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Edita un repostaje</h2> | |||||
| <form method="post"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <?php foreach ($kms as $key => $value) : ?> | |||||
| <label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?></label> | |||||
| <input type="text" name="<?php echo $key; ?>" identificador="<?php echo $key; ?>" | |||||
| value="<?php echo escape($value); ?>" <?php echo ($key === 'identificador' ? 'readonly' : null); ?>> | |||||
| <?php endforeach; ?> | |||||
| <input type="submit" name="submit" value="Submit"> | |||||
| </form> | |||||
| </section> | |||||
| <a href="menu.php" class="button small">Volver</a> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,66 @@ | |||||
| <?php | |||||
| /** | |||||
| * List all vehiculos with a link to edit | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $sql = "SELECT * FROM carburante ORDER BY fecha DESC"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <section id="five" class="main style1"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Actualizar repostajes</h2> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Fecha</th> | |||||
| <th>Vehículo</th> | |||||
| <th>Kilómetros</th> | |||||
| <th>Litros</th> | |||||
| <th>Descuento</th> | |||||
| <th>Precio/litro</th> | |||||
| <th>Importe</th> | |||||
| <th>Editar</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php foreach ($result as $row) : ?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["fecha"]); ?></td> | |||||
| <td><?php echo escape($row["vehiculo"]); ?></td> | |||||
| <td><?php echo escape($row["kms"]); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["litros"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["descuento"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["precioxlitro"])); ?></td> | |||||
| <td><?php echo escape(str_replace('.', ',',$row["importe"])); ?></td> | |||||
| <td><a | |||||
| href="update-single.php?identificador=<?php echo escape($row["identificador"]); ?>">Edit</a> | |||||
| </td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,5 @@ | |||||
| </body> | |||||
| <footer> | |||||
| <p>Gestión de vehículos, Copyright © C.Rey 2023</p> | |||||
| </footer> | |||||
| </html> | |||||
| @ -0,0 +1,60 @@ | |||||
| <!doctype html> | |||||
| <html lang="en"> | |||||
| <head> | |||||
| <meta charset="utf-8"> | |||||
| <meta http-equiv="x-ua-compatible" content="ie=edge"> | |||||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |||||
| <title>App de Gestión de Vehículos</title> | |||||
| <link rel="stylesheet" href="/css/style.css" /> | |||||
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> | |||||
| </head> | |||||
| <body> | |||||
| <header> | |||||
| <div class="container"> | |||||
| <div class="branding"> | |||||
| <h1>Gestión de <span class="highlight">Vehículos</span></h1> | |||||
| </div> | |||||
| <!-- | |||||
| <nav> | |||||
| <ul> | |||||
| <li><a href="/gestionrepostajes/index.php">Inicio</a></li> | |||||
| <li><a href="/gestionrepostajes/vehiculos/menu.php">Vehículos</a></li> | |||||
| <li><a href="/gestionrepostajes/repostajes/menu.php">Repostajes</a></li> | |||||
| </ul> | |||||
| </nav> | |||||
| --> | |||||
| <div class="navbar"> | |||||
| <a href="/gestionrepostajes/index.php">Inicio</a> | |||||
| <!-- Menú de vehículos --> | |||||
| <div class="subnav"> | |||||
| <button class="subnavbtn">Vehículos<i class="fa fa-caret-down"></i></button> | |||||
| <div class="subnav-content"> | |||||
| <a href="/gestionrepostajes/vehiculos/create.php"><strong>Añadir un vehículo</strong></a> | |||||
| <a href="/gestionrepostajes/vehiculos/read.php"><strong>Listar vehículos</strong></a> | |||||
| <a href="/gestionrepostajes/vehiculos/update.php"><strong>Actualizar vehículos</strong></a> | |||||
| <a href="/gestionrepostajes/vehiculos/delete.php"><strong>Borrar vehículos</strong></a> | |||||
| </div> | |||||
| </div> | |||||
| <!-- Menú de repostajes --> | |||||
| <div class="subnav"> | |||||
| <button class="subnavbtn">Repostajes<i class="fa fa-caret-down"></i></button> | |||||
| <div class="subnav-content"> | |||||
| <a href="/gestionrepostajes/repostajes/create.php"><strong>Crear un repostaje</strong></a> | |||||
| <a href="/gestionrepostajes/repostajes/read.php"><strong>Mostrar repostajes</strong></a> | |||||
| <a href="/gestionrepostajes/repostajes/update.php"><strong>Actualizar repostajes</strong></a> | |||||
| <a href="/gestionrepostajes/repostajes/delete.php"><strong>Borrar repostajes</strong></a> | |||||
| </div> | |||||
| </div> | |||||
| </div> | |||||
| </div> | |||||
| </header> | |||||
| @ -0,0 +1,63 @@ | |||||
| <?php | |||||
| /** | |||||
| * Use an HTML form to create a new entry in the | |||||
| * users table. | |||||
| * | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| if (isset($_POST['submit'])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $nuevo_coche = array( | |||||
| "marca" => $_POST['marca'], | |||||
| "modelo" => $_POST['modelo'], | |||||
| "matricula" => $_POST['matricula'] | |||||
| ); | |||||
| $sql = sprintf( | |||||
| "INSERT INTO %s (%s) values (%s)", | |||||
| "vehiculos", | |||||
| implode(", ", array_keys($nuevo_coche)), | |||||
| ":" . implode(", :", array_keys($nuevo_coche)) | |||||
| ); | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute($nuevo_coche); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <?php if (isset($_POST['submit']) && $statement) : ?> | |||||
| <blockquote><?php echo escape($_POST['marca']); ?> successfully added.</blockquote> | |||||
| <?php endif; ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Añadir vehículo</h2> | |||||
| <form method="post" class="formulario"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <p><label for="marca">Marca</label></p> | |||||
| <p><input type="text" name="marca" id="marca" required></p> | |||||
| <p><label for="modelo">Modelo</label></p> | |||||
| <p><input type="text" name="modelo" id="modelo" required></p> | |||||
| <p><label for="matricula">Matricula</label></p> | |||||
| <p><input type="text" name="matricula" id="matricula" required></p> | |||||
| <br> | |||||
| <p><input type="submit" name="submit" value="Crear" class="primary"></p> | |||||
| </form> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,84 @@ | |||||
| <?php | |||||
| /** | |||||
| * Delete a user | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| $success = null; | |||||
| if (isset($_POST["submit"])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $identificador = $_POST["submit"]; | |||||
| $sql = "DELETE FROM vehiculos WHERE identificador = :identificador"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->bindValue(':identificador', $identificador); | |||||
| $statement->execute(); | |||||
| $success = "User successfully deleted"; | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $sql = "SELECT * FROM vehiculos"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <section id="five" class="main style1"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Borrar vehículos</h2> | |||||
| <?php if ($success) echo $success; ?> | |||||
| <form method="post"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Marca</th> | |||||
| <th>Modelo</th> | |||||
| <th>Matrícula</th> | |||||
| <th>Borrar</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php foreach ($result as $row) : ?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["marca"]); ?></td> | |||||
| <td><?php echo escape($row["modelo"]); ?></td> | |||||
| <td><?php echo escape($row["matricula"]); ?></td> | |||||
| <td><button type="submit" name="submit" | |||||
| value="<?php echo escape($row["identificador"]); ?>">Borrar</button></td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </form> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,22 @@ | |||||
| <?php include "../templates/header.php"; ?> | |||||
| <section id="boxes"> | |||||
| <div class="container"> | |||||
| <div class="box"> | |||||
| <a href="create.php"><strong>Añadir un vehículo</strong></a> | |||||
| </div> | |||||
| <div class="box"> | |||||
| <a href="read.php"><strong>Listar vehículos</strong></a> | |||||
| </div> | |||||
| <div class="box"> | |||||
| <a href="update.php"><strong>Actualizar vehículos</strong></a> | |||||
| </div> | |||||
| <div class="box"> | |||||
| <a href="delete.php"><strong>Borrar vehículos</strong></a> | |||||
| </div> | |||||
| </div> | |||||
| </section> | |||||
| <br> | |||||
| <?php include "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,88 @@ | |||||
| <?php | |||||
| /** | |||||
| * Function to query information based on | |||||
| * a parameter: in this case, marca. | |||||
| * | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| if (isset($_POST['submit'])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $sql = "SELECT * | |||||
| FROM vehiculos | |||||
| WHERE marca = :marca"; | |||||
| $marca = $_POST['marca']; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->bindParam(':marca', $marca, PDO::PARAM_STR); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <?php | |||||
| if (isset($_POST['submit'])) { | |||||
| if ($result && $statement->rowCount() > 0) { ?> | |||||
| <section id="five" class="main style1"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Resultados</h2> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Marca</th> | |||||
| <th>Modelo</th> | |||||
| <th>Matrícula</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php foreach ($result as $row) : ?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["marca"]); ?></td> | |||||
| <td><?php echo escape($row["modelo"]); ?></td> | |||||
| <td><?php echo escape($row["matricula"]); ?></td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php } else { ?> | |||||
| <blockquote>No results found for <?php echo escape($_POST['marca']); ?>.</blockquote> | |||||
| <?php } | |||||
| } ?> | |||||
| <section class="principal"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Busca vehículo por marca</h2> | |||||
| <form method="post"> | |||||
| <div class="row gtr-uniform gtr-50"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <p><label for="marca">Marca</label></p> | |||||
| <p><input type="text" id="marca" name="marca"></p> | |||||
| <input type="submit" name="submit" value="Ver resultados" class="primary"> | |||||
| </div> | |||||
| </form> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,84 @@ | |||||
| <?php | |||||
| /** | |||||
| * Use an HTML form to edit an entry in the | |||||
| * vehiculos table. | |||||
| * | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| if (isset($_POST['submit'])) { | |||||
| if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die(); | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $vehiculo =[ | |||||
| "identificador" => $_POST['identificador'], | |||||
| "marca" => $_POST['marca'], | |||||
| "modelo" => $_POST['modelo'], | |||||
| "matricula" => $_POST['matricula'] | |||||
| ]; | |||||
| $sql = "UPDATE vehiculos | |||||
| SET identificador = :identificador, | |||||
| marca = :marca, | |||||
| modelo = :modelo, | |||||
| matricula = :matricula | |||||
| WHERE identificador = :identificador"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute($vehiculo); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } | |||||
| if (isset($_GET['identificador'])) { | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $identificador = $_GET['identificador']; | |||||
| $sql = "SELECT * FROM vehiculos WHERE identificador = :identificador"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->bindValue(':identificador', $identificador); | |||||
| $statement->execute(); | |||||
| $vehiculo = $statement->fetch(PDO::FETCH_ASSOC); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| } else { | |||||
| echo "¡Algo ha salido mal!"; | |||||
| exit; | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <?php if (isset($_POST['submit']) && $statement) : ?> | |||||
| <blockquote><?php echo escape($_POST['marca']); ?> successfully updated.</blockquote> | |||||
| <?php endif; ?> | |||||
| <section id="five" class="main style1"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Edita un vehículo</h2> | |||||
| <form method="post"> | |||||
| <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>"> | |||||
| <?php foreach ($vehiculo as $key => $value) : ?> | |||||
| <label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?></label> | |||||
| <input type="text" name="<?php echo $key; ?>" identificador="<?php echo $key; ?>" | |||||
| value="<?php echo escape($value); ?>" <?php echo ($key === 'identificador' ? 'readonly' : null); ?>> | |||||
| <?php endforeach; ?> | |||||
| <br> | |||||
| <input type="submit" name="submit" class="primary" value="Submit"> | |||||
| </form> | |||||
| <a href="menu.php" class="button small" >Volver</a> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||
| @ -0,0 +1,57 @@ | |||||
| <?php | |||||
| /** | |||||
| * List all vehiculos with a link to edit | |||||
| */ | |||||
| require "../config.php"; | |||||
| require "../common.php"; | |||||
| try { | |||||
| $connection = new PDO($dsn, $username, $password, $options); | |||||
| $sql = "SELECT * FROM vehiculos"; | |||||
| $statement = $connection->prepare($sql); | |||||
| $statement->execute(); | |||||
| $result = $statement->fetchAll(); | |||||
| } catch(PDOException $error) { | |||||
| echo $sql . "<br>" . $error->getMessage(); | |||||
| } | |||||
| ?> | |||||
| <?php require "../templates/header.php"; ?> | |||||
| <section id="five" class="main style1"> | |||||
| <div class="container"> | |||||
| <section> | |||||
| <h2>Actualizar vehículos</h2> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>#</th> | |||||
| <th>Marca</th> | |||||
| <th>Modelo</th> | |||||
| <th>Matrícula</th> | |||||
| <th>Editar</th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <?php foreach ($result as $row) : ?> | |||||
| <tr> | |||||
| <td><?php echo escape($row["identificador"]); ?></td> | |||||
| <td><?php echo escape($row["marca"]); ?></td> | |||||
| <td><?php echo escape($row["modelo"]); ?></td> | |||||
| <td><?php echo escape($row["matricula"]); ?></td> | |||||
| <td><a | |||||
| href="update-single.php?identificador=<?php echo escape($row["identificador"]); ?>">Edit</a> | |||||
| </td> | |||||
| </tr> | |||||
| <?php endforeach; ?> | |||||
| </tbody> | |||||
| </table> | |||||
| </section> | |||||
| </div> | |||||
| </section> | |||||
| <?php require "../templates/footer.php"; ?> | |||||