-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
122 lines (105 loc) · 4 KB
/
edit.php
File metadata and controls
122 lines (105 loc) · 4 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// Include database connection and helper functions
require_once 'config/db.php';
require_once 'includes/functions.php';
// Initialize variables
$errors = [];
$contact = [
'id' => '',
'first_name' => '',
'last_name' => '',
'phone' => '',
'email' => ''
];
// Check if ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
// Redirect to index if no ID provided
header('Location: index.php');
exit;
}
$id = (int)$_GET['id'];
// Get contact data
$contact = getContactById($id);
// If contact not found, redirect to index
if (!$contact) {
header('Location: index.php');
exit;
}
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Sanitize input data
$updatedContact = [
'first_name' => trim($_POST['first_name']),
'last_name' => trim($_POST['last_name']),
'phone' => trim($_POST['phone']),
'email' => trim($_POST['email'])
];
// Validate contact data
$errors = validateContactData($updatedContact);
// If no errors, update contact in database
if (empty($errors)) {
if (updateContact($id, $updatedContact)) {
// Redirect to index page on success
header('Location: index.php');
exit;
} else {
$errors['db'] = "Failed to update the contact. Please try again.";
}
}
// Update contact variable with form data for re-display
$contact = array_merge($contact, $updatedContact);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Contact - PHP Contacts App</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="container">
<h1>Edit Contact</h1>
<a href="index.php" class="back-link">Back to Contacts</a>
<?php if (isset($errors['db'])): ?>
<div class="alert alert-error">
<?php echo $errors['db']; ?>
</div>
<?php endif; ?>
<form action="edit.php?id=<?php echo $id; ?>" method="POST" class="contact-form">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" value="<?php echo htmlspecialchars($contact['first_name']); ?>" required>
<?php if (isset($errors['first_name'])): ?>
<div class="error"><?php echo $errors['first_name']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" value="<?php echo htmlspecialchars($contact['last_name']); ?>" required>
<?php if (isset($errors['last_name'])): ?>
<div class="error"><?php echo $errors['last_name']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" value="<?php echo htmlspecialchars($contact['phone']); ?>" required>
<?php if (isset($errors['phone'])): ?>
<div class="error"><?php echo $errors['phone']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($contact['email']); ?>" required>
<?php if (isset($errors['email'])): ?>
<div class="error"><?php echo $errors['email']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<button type="submit" class="btn">Update Contact</button>
</div>
</form>
</div>
</body>
</html>