-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.php
96 lines (79 loc) · 2.8 KB
/
edit.php
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();
if (!isset($_SESSION["user"])) {
header("Location: login.php");
return;
}
$id = $_GET["id"];
$statement = $conn->prepare("SELECT * FROM contacts WHERE id = :id LIMIT 1");
$statement->execute([":id" => $id]);
if ($statement->rowCount() == 0) {
http_response_code(404);
echo("NOT FOUND");
return;
}
$contact = $statement->fetch(PDO::FETCH_ASSOC);
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 the fields";
} else if (strlen($_POST["phone_number"]) < 10) {
$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" => $name,
":phone_number" => $phoneNumber
]);
$_SESSION["flash"] = ["message" => "Contact {$_POST['name']} updated."];
header("Location: home.php");
return;
}
}
?>
<?php require "partials/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 != null): ?>
<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 value="<?= $contact["name"] ?>" id="name" type="text" class="form-control" name="name" 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 value="<?= $contact["phone_number"] ?>" id="phone_number" type="tel" class="form-control" name="phone_number" 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 "partials/footer.php";