### Estructura de archivos sugerida:
```
/crud-pacientes
├── conexion.php
├── index.php ← Mostrar pacientes
├── agregar.php ← Formulario para agregar
├── editar.php ← Formulario para editar
├── eliminar.php ← Lógica para eliminar
```
---
### ✅ `index.php` (Listado de pacientes)
```php
<?php include 'conexion.php'; ?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Lista de Pacientes</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial; padding: 20px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px; border: 1px solid #ccc; }
a.button { padding: 6px 12px; background: #007bff; color: #fff; text-decoration: none;
border-radius: 5px; }
a.button:hover { background: #0056b3; }
@media(max-width: 600px){
table, thead, tbody, th, td, tr {
display: block;
th, td { padding: 10px 5px; text-align: left; }
</style>
</head>
<body>
<h1>Pacientes</h1>
<a href="agregar.php" class="button">Agregar Paciente</a>
<br><br>
<table>
<thead>
<tr>
<th>Nombre</th>
<th>Género</th>
<th>Edad</th>
<th>Teléfono</th>
<th>Dirección</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM pacientes";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['nombre_completo']}</td>
<td>{$row['genero']}</td>
<td>{$row['edad']}</td>
<td>{$row['telefono']}</td>
<td>{$row['direccion']}</td>
<td>
<a href='editar.php?id={$row['id']}' class='button'>Editar</a>
<a href='eliminar.php?id={$row['id']}' class='button' onclick='return confirm(\"¿Eliminar
paciente?\")'>Eliminar</a>
</td>
</tr>";
?>
</tbody>
</table>
</body>
</html>
```
---
### ✅ `agregar.php` (Formulario para agregar)
```php
<?php include 'conexion.php'; ?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Agregar Paciente</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
form { max-width: 600px; margin: auto; display: flex; flex-direction: column; gap: 10px; }
input, select, textarea { padding: 10px; font-size: 1em; }
button { padding: 10px; background: green; color: white; border: none; }
a { text-align: center; display: block; margin-top: 10px; }
</style>
</head>
<body>
<h2>Agregar Nuevo Paciente</h2>
<form method="POST" action="">
<input type="text" name="nombre_completo" placeholder="Nombre completo" required>
<select name="genero" required>
<option value="">Seleccione género</option>
<option value="Masculino">Masculino</option>
<option value="Femenino">Femenino</option>
</select>
<input type="number" name="edad" placeholder="Edad" required>
<input type="text" name="telefono" placeholder="Teléfono" required>
<textarea name="direccion" placeholder="Dirección" required></textarea>
<button type="submit">Guardar</button>
</form>
<a href="index.php">Volver</a>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nombre = $_POST['nombre_completo'];
$genero = $_POST['genero'];
$edad = $_POST['edad'];
$telefono = $_POST['telefono'];
$direccion = $_POST['direccion'];
$sql = "INSERT INTO pacientes (nombre_completo, genero, edad, telefono, direccion)
VALUES ('$nombre', '$genero', '$edad', '$telefono', '$direccion')";
if ($conn->query($sql)) {
header("Location: index.php");
} else {
echo "Error: " . $conn->error;
?>
</body>
</html>
```
---
### ✅ `editar.php` (Formulario para editar)
```php
<?php include 'conexion.php'; ?>
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM pacientes WHERE id = $id";
$result = $conn->query($sql);
$paciente = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Editar Paciente</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
form { max-width: 600px; margin: auto; display: flex; flex-direction: column; gap: 10px; }
input, select, textarea { padding: 10px; font-size: 1em; }
button { padding: 10px; background: orange; color: white; border: none; }
a { text-align: center; display: block; margin-top: 10px; }
</style>
</head>
<body>
<h2>Editar Paciente</h2>
<form method="POST" action="">
<input type="text" name="nombre_completo" value="<?= $paciente['nombre_completo'] ?
>" required>
<select name="genero" required>
<option value="Masculino" <?= $paciente['genero'] == 'Masculino' ? 'selected' : '' ?
>>Masculino</option>
<option value="Femenino" <?= $paciente['genero'] == 'Femenino' ? 'selected' : '' ?
>>Femenino</option>
</select>
<input type="number" name="edad" value="<?= $paciente['edad'] ?>" required>
<input type="text" name="telefono" value="<?= $paciente['telefono'] ?>" required>
<textarea name="direccion" required><?= $paciente['direccion'] ?></textarea>
<button type="submit">Actualizar</button>
</form>
<a href="index.php">Volver</a>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nombre = $_POST['nombre_completo'];
$genero = $_POST['genero'];
$edad = $_POST['edad'];
$telefono = $_POST['telefono'];
$direccion = $_POST['direccion'];
$sql = "UPDATE pacientes SET
nombre_completo='$nombre', genero='$genero', edad=$edad,
telefono='$telefono', direccion='$direccion'
WHERE id=$id";
if ($conn->query($sql)) {
header("Location: index.php");
} else {
echo "Error: " . $conn->error;
?>
</body>
</html>
```
---
### ✅ `eliminar.php` (Lógica para eliminar)
```php
<?php
include 'conexion.php';
$id = $_GET['id'];
$sql = "DELETE FROM pacientes WHERE id = $id";
if ($conn->query($sql)) {
header("Location: index.php");
} else {
echo "Error: " . $conn->error;
?>
```
---