-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.php
75 lines (63 loc) · 2.33 KB
/
add.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
<?php
require "database.php";
session_start();
if (!isset($_SESSION["user"])) {
header("Location: login.php");
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("INSERT INTO contacts (user_id, name, phone_number) VALUES ({$_SESSION['user']['id']}, :name, :phone_number)");
$statement->bindParam(":name", $_POST["name"]);
$statement->bindParam(":phone_number", $_POST["phone_number"]);
$statement->execute();
$_SESSION["flash"] = ["message" => "Contact {$_POST['name']} added."];
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="add.php">
<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" 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 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";