-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
87 lines (72 loc) · 1.98 KB
/
edit.php
File metadata and controls
87 lines (72 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require 'config.php';
$id = (int)($_GET['id'] ?? 0);
if (!$id) {
header('Location: index.php');
exit;
}
$stmt = $pdo->prepare("SELECT * FROM contacts WHERE id = :id");
$stmt->execute([':id' => $id]);
$contact = $stmt->fetch();
if (!$contact) {
header('Location: index.php');
exit;
}
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
if ($name === '' || $email === '') {
$errors[] = "Nombre y email son obligatorios.";
}
if (empty($errors)) {
$sql = "UPDATE contacts
SET name = :name, email = :email, phone = :phone
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':name' => $name,
':email' => $email,
':phone' => $phone,
':id' => $id
]);
header('Location: index.php');
exit;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Editar contacto</title>
</head>
<body>
<h1>Editar contacto</h1>
<?php if (!empty($errors)): ?>
<?php foreach ($errors as $error): ?>
<p style="color:red;"><?= htmlspecialchars($error) ?></p>
<?php endforeach; ?>
<?php endif; ?>
<form method="post">
<label>
Nombre<br>
<input type="text" name="name" value="<?= htmlspecialchars($contact['name']) ?>" required>
</label>
<br><br>
<label>
Email<br>
<input type="email" name="email" value="<?= htmlspecialchars($contact['email']) ?>" required>
</label>
<br><br>
<label>
Teléfono<br>
<input type="text" name="phone" value="<?= htmlspecialchars($contact['phone']) ?>">
</label>
<br><br>
<button type="submit">Actualizar</button>
<a href="index.php">Cancelar</a>
</form>
</body>
</html>