SQL
This commit is contained in:
102
Practicas/Practicas_SQL/ejercicios/tienda/realizar_pedido.php
Normal file
102
Practicas/Practicas_SQL/ejercicios/tienda/realizar_pedido.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
// Comprobamos si existe la sesión
|
||||
session_start();
|
||||
if (!isset($_SESSION['cliente_id'])) {
|
||||
// En caso contrario devolvemos a la página login.php
|
||||
header('Location: login.php');
|
||||
die();
|
||||
}
|
||||
|
||||
$cliente_id = $_SESSION['cliente_id'];
|
||||
$nombre_servidor = "localhost";
|
||||
$nombre_usuario = "root";
|
||||
$contraseña = "Logomark8";
|
||||
$nombre_base_datos = "tienda";
|
||||
|
||||
mysqli_report(MYSQLI_REPORT_OFF);
|
||||
$conexion = mysqli_connect($nombre_servidor, $nombre_usuario, $contraseña, $nombre_base_datos);
|
||||
if (!$conexion) {
|
||||
exit("Error de conexión: " . mysqli_connect_error());
|
||||
}
|
||||
|
||||
$consulta_sql = "SELECT * FROM productos";
|
||||
$resultado = mysqli_query($conexion, $consulta_sql);
|
||||
if (!$resultado) {
|
||||
exit("Error al ejecutar la consulta: " . mysqli_error($conexion));
|
||||
}
|
||||
|
||||
$productos = mysqli_fetch_all($resultado, MYSQLI_ASSOC);
|
||||
|
||||
mysqli_free_result($resultado);
|
||||
mysqli_close($conexion);
|
||||
?>
|
||||
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="estilo.css">
|
||||
<title>Registro Pedidos</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>Registrar Pedido</h2>
|
||||
<form action="" method="post">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Descripcion</th>
|
||||
<th>Precio</th>
|
||||
<th>Cantidad</th>
|
||||
</tr>
|
||||
<?php
|
||||
foreach ($productos as $producto) {
|
||||
echo "<tr>";
|
||||
echo "<td>$producto[nombre] </td> <td>$producto[precio] € </td>";
|
||||
echo "<td hidden> <input type='number' id='producto_id' name='producto_id[]' value=$producto[producto_id]></td>";
|
||||
echo "<td hidden> <input type='number' id='precio' name='precio[]' value=$producto[precio]></td>";
|
||||
echo "<td> <input type='number' id='cantidad' name='cantidad[]' value=0></td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
?>
|
||||
</table>
|
||||
<br>
|
||||
<input type="submit" value="Registrar Pedido">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
<?php
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
|
||||
$lineas_pedido = array();
|
||||
$total_pedido = 0;
|
||||
|
||||
foreach (array_keys($_POST['producto_id']) as $index) {
|
||||
if ($_POST['cantidad'][$index] > 0) {
|
||||
$lineas_pedido[] = array(
|
||||
'producto_id' => intval($_POST['producto_id'][$index]),
|
||||
'cantidad' => intval($_POST['cantidad'][$index]),
|
||||
'precio' => floatval($_POST['precio'][$index]),
|
||||
'total_linea' => $_POST['cantidad'][$index] * $_POST['precio'][$index]
|
||||
);
|
||||
$total_pedido += $_POST['cantidad'][$index] * $_POST['precio'][$index];
|
||||
}
|
||||
}
|
||||
|
||||
$cliente_id = $_SESSION['cliente_id'];
|
||||
$nombre_servidor = "localhost";
|
||||
$nombre_usuario = "root";
|
||||
$contraseña = "Logomark8";
|
||||
$nombre_base_datos = "tienda";
|
||||
|
||||
mysqli_report(MYSQLI_REPORT_OFF);
|
||||
$conexion = mysqli_connect($nombre_servidor, $nombre_usuario, $contraseña, $nombre_base_datos);
|
||||
if (!$conexion) {
|
||||
exit("Error de conexión: " . mysqli_connect_error());
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user