-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
96 lines (81 loc) · 2.82 KB
/
Copy pathedit.php
File metadata and controls
96 lines (81 loc) · 2.82 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
<?php
require "database.php";
session_start();
/**< Detect if user loged */
if (!isset($_SESSION["user"])) {
header("Location: login.php");
return;
}
// Get identifier from GET/POST method to obtain contact information
$statement = $conn->prepare("SELECT * FROM contacts WHERE id = :id LIMIT 1");
$statement->execute([":id" => $_GET["id"]]);
if ($statement->rowCount() == 0) {
http_response_code(404);
echo("HTTP 404 NOT FOUND\n");
return;
}
$contact = $statement->fetch(PDO::FETCH_ASSOC);
/**< Does not allow user with different ID to edit a contact */
if ($contact["user_id"] !== $_SESSION["user"]["id"]) {
http_response_code(403);
echo("HTTP 403 UNAUTHORIZED");
return;
}
$error = null;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ( (empty($_POST["name"])) || (empty($_POST["phone_number"])) ) {
$error = "Please fill all fields.";
}
else if ( strlen($_POST["phone_number"]) < 9 ) {
$error = "Phone number must be at least 9 characters.";
}
else {
$name = $_POST["name"];
$phoneNumber = $_POST["phone_number"];
$statement = $conn->prepare("UPDATE contacts SET name = :name, phone_number = :phone_number WHERE id = :id");
$statement->execute([
":id" => $id,
":name" => $_POST["name"],
":phone_number" => $_POST["phone_number"],
]);
header("Location: home.php");
}
}
?>
<?php require "partial/header.php" ?>
<div class="container pt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Add New Contact</div>
<div class="card-body">
<?php if ($error): ?>
<p class="text-danger">
<?= $error ?>
</p>
<?php endif ?>
<form method="POST" action="edit.php?id=<?= $contact["id"] ?>">
<div class="mb-3 row">
<label for="name" class="col-md-4 col-form-label text-md-end">Name</label>
<div class="col-md-6">
<input id="name" value="<?= $contact["name"] ?>" type="text" class="form-control" name="name" required autocomplete="name" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="phone_number" class="col-md-4 col-form-label text-md-end">Phone Number</label>
<div class="col-md-6">
<input id="phone_number" value="<?= $contact["phone_number"] ?>" type="tel" class="form-control" name="phone_number" required autocomplete="phone_number" autofocus>
</div>
</div>
<div class="mb-3 row">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php require "partial/footer.php" ?>