IFCD0210/Practicas/Practicas_SQL/ejercicios/tienda/crud_clientes.php
2024-03-20 13:43:42 +01:00

89 lines
2.7 KiB
PHP

<?php
$variable = 'texto boton';
$clientes = getClientes();
function conectBD()
{
$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());
}
return $conexion;
}
function getClientes()
{
$conexion = conectBD();
$consulta_sql = "SELECT c.*, u.* FROM usuarios u LEFT JOIN clientes c ON u.cliente_id = c.cliente_id;";
$resultado = mysqli_query($conexion, $consulta_sql);
if (!$resultado) {
exit("Error al ejecutar la consulta: " . mysqli_error($conexion));
}
$clientes = mysqli_fetch_all($resultado, MYSQLI_ASSOC);
mysqli_free_result($resultado);
mysqli_close($conexion);
return $clientes;
}
?>
<!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>Crud Clientes</title>
</head>
<body>
<form action="">
<table style="width:100%;">
<tr>
<th>Nombre</th>
<th>Email</th>
<th>Direccion</th>
<th>UserName</th>
<th>Password</th>
<th></th>
</tr>
<tr>
<td><input type="text" name="nombre" id=""></td>
<td><input type="text" name="email" id=""></td>
<td><input type="text" name="direccion" id=""></td>
<td><input type="text" name="username" id=""></td>
<td><input type="password" name="password" id=""></td>
<td><input style="margin-bottom: 20px; padding:11px 20px;" type="submit" value="<?= $variable ?>"></td>
</tr>
</table>
</form>
<table style="width:100%;">
<?php if (isset($clientes) && !empty($clientes)) : ?>
<?php foreach ($clientes as $cliente) : ?>
<tr>
<td><?= $cliente['nombre'] ?></td>
<td><?= $cliente['correo_electronico'] ?></td>
<td><?= $cliente['direccion'] ?></td>
<td><?= $cliente['nombre_usuario'] ?></td>
<td><?= $cliente['contraseña'] ?></td>
<td> </td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="6" style="text-align:center;">
<p style="font-size: 3rem;">No hay clientes</p>
</td>
</tr>
<?php endif; ?>
</table>
</body>
</html>