-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
136 lines (119 loc) · 4.72 KB
/
create.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
//require_once "iconn.php";
require "conn.php";
//Create Connection
$dbh = new conn();
//start as empty fields
$name = "";
$email = "";
$phone = "";
$address = "";
$errorMessage = "";
$successMessage = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
do {
//None field can be empty
if (empty($name) || empty($email) || empty($phone) || empty($address)) {
$errorMessage = "All fields must be fill";
break;
}
$stmt = $dbh->query("INSERT INTO persons (name, email, phone, address) VALUES ('$name', '$email', '$phone', '$address')");
$result = $stmt->execute();
if (!$result) {
$errorMessage = "Invalid query: " . $conn->error;
break;
} //Error Message
//initializing the empty fields again
$name = "";
$email = "";
$phone = "";
$address = "";
//Success Message
$successMessage = "Successfuly addded person";
//back to index page
header("location: index.php");
exit;
} while (false);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link rel="shortcut icon" type="image/x-ico" href="favicon_io/favicon-16x16.png">
<title>2º CRUD - Add New Person</title>
</head>
<body>
<div class="container my-5">
<h2>New Person</h2>
<!--Error Message-->
<?php
if (!empty($errorMessage)) {
echo "
<div class='alert alert-warning alert-dismissible fade show' role='alert'>
<strong>$errorMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>
";
}
?>
<form method="POST">
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="name" value="<?php echo $name ?>">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="email" value="<?php echo $email ?>">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Phone</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="phone" value="<?php echo $phone ?>">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Address</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="address" value="<?php echo $address ?>">
</div>
</div>
<!--Success Message-->
<?php
if (!empty($successMessage)) {
echo "
<div class='row mb-3'
<div class='offset-sm-3 col-sm-6'>
<div class='alert alert-success alert-dismissible fade show' role='alert'>
<strong>$successMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>
</div>
</div>
";
}
?>
<div class="row mb-3">
<div class="offset-sm-3 col-sm-3 d-grid">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="col-sm-3 d-grid">
<a class="btn btn-outline-primary" href=" index.php" role="button">Cancel</a>
</div>
</div>
</form>
</div>
</body>
</html>