¡Claro, pana! Aquí te dejo un ejemplo básico de un CRUD en PHP con MySQLi, HTML y CSS. Este ejemplo muestra cómo crear, leer, actualizar y eliminar registros de una base de datos. Recuerda que debes crear la base de datos y la tabla antes de probar este código.
Antes de comenzar, asegúrate de tener instalado PHP y MySQL en tu servidor.
index.php:
```html
<!DOCTYPE html>
<html>
<head>
<title>CRUD en PHP</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>CRUD en PHP con MySQLi</h1>
<?php
// Conexión a la base de datos
$mysqli = new mysqli("localhost", "usuario", "contraseña", "basededatos");
// Mostrar un error si no se puede conectar
if ($mysqli -> connect_errno) {
echo "Error al conectarse a la base de datos: " . $mysqli -> connect_error;
exit();
}
// Obtener todos los registros
$resultado = $mysqli -> query("SELECT * FROM usuarios");
// Mostrar los registros en una tabla HTML
if ($resultado -> num_rows > 0) {
echo "<table>";
echo "<tr><th>ID</th><th>Nombre</th><th>Correo electrónico</th><th></th></tr>";
while ($fila = $resultado -> fetch_assoc()) {
echo "<tr>";
echo "<td>" . $fila["id"] . "</td>";
echo "<td>" . $fila["nombre"] . "</td>";
echo "<td>" . $fila["correo"] . "</td>";
echo "<td><a href='editar.php?id=" . $fila["id"] . "'>Editar</a> <a href='eliminar.php?id=" . $fila["id"] . "'>Eliminar</a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No se encontraron registros.";
}
// Cerrar la conexión
$resultado -> free_result();
$mysqli -> close();
?>
<p><a href="crear.php">Crear nuevo registro</a></p>
</body>
</html>
```
crear.php:
```html
<!DOCTYPE html>
<html>
<head>
<title>Crear registro - CRUD en PHP</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Crear registro - CRUD en PHP con MySQLi</h1>
<?php
// Verificar si se recibieron datos del formulario
if (isset($_POST["nombre"]) && isset($_POST["correo"])) {
// Conexión a la base de datos
$mysqli = new mysqli("localhost", "usuario", "contraseña", "basededatos");
// Mostrar un error si no se puede conectar
if ($mysqli -> connect_errno) {
echo "Error al conectarse a la base de datos: " . $mysqli -> connect_error;
exit();
}
// Obtener los datos del formulario
$nombre = $_POST["nombre"];
$correo = $_POST["correo"];
// Insertar los datos en la tabla
$resultado = $mysqli -> query("INSERT INTO usuarios (nombre, correo) VALUES ('$nombre', '$correo')");
// Mostrar un mensaje de éxito o error
if ($resultado) {
echo "<p>Registro creado exitosamente.</p>";
} else {
echo "<p>Error al crear el registro: " . $mysqli -> error . "</p>";
}
// Cerrar la conexión
$mysqli -> close();
}
?>
<form action="crear.php" method="POST">
<label for="nombre">Nombre:</label>
<input type="text" name="nombre" id="nombre" required><br>
<label for="correo">Correo electrónico:</label>
<input type="email" name="correo" id="correo" required><br>
<input type="submit" value="Crear registro">
</form>
<p><a href="index.php">Volver</a></p>
</body>
</html>
```
editar.php:
```html
<!DOCTYPE html>
<html>
<head>
<title>Editar registro - CRUD en PHP</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Editar registro - CRUD en PHP con MySQLi</h1>
<?php
// Verificar si se recibió el ID del registro a editar
if (isset($_GET["id"])) {
// Conexión a la base de datos
$mysqli = new mysqli("localhost", "usuario", "contraseña", "basededatos");
// Mostrar un error si no se puede conectar
if ($mysqli -> connect_errno) {
echo "Error al conectarse a la base de datos: " . $mysqli -> connect_error;
exit();
}
// Obtener los datos del registro a editar
$id = $_GET["id"];
$resultado = $mysqli -> query("SELECT * FROM usuarios WHERE id = $id");
if ($fila = $resultado -> fetch_assoc()) {
// Mostrar el formulario prellenado con los datos del registro a editar
echo "<form action='actualizar.php?id=" . $fila["id"] . "' method='POST'>";
echo "<label for='nombre'>Nombre:</label>";
echo "<input type='text' name='nombre' id='nombre' value='" . $fila["nombre"] . "' required><br>";
echo "<label for='correo'>Correo electrónico:</label>";
echo "<input type='email' name='correo' id='correo' value='" . $fila["correo"] . "' required><br>";
echo "<input type='submit' value='Guardar cambios'>";
echo "</form>";
} else {
echo "<p>Registro no encontrado.</p>";
}
// Cerrar la conexión
$resultado -> free_result();
$mysqli -> close();
} else {
echo "<p>ID del registro no especificado.</p>";
}
?>
<p><a href="index.php">Volver</a></p>
</body>
</html>
```
actualizar.php:
```html
<!DOCTYPE html>
<html>
<head>
<title>Actualizar registro - CRUD en PHP</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Actualizar registro - CRUD en PHP con MySQLi</h1>
<?php
// Verificar si se recibieron datos del formulario
if (isset($_POST["nombre"]) && isset($_POST["correo"]) && isset($_GET["id"])) {
// Conexión a la base de datos
$mysqli = new mysqli("localhost", "usuario", "contraseña", "basededatos");
// Mostrar un error si