-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-barber.php
265 lines (237 loc) · 12.8 KB
/
create-barber.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!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">
<title>Create Barber</title>
<?php
$activeMenu="accounts";
include_once("includes/admin-menu.php");
$fnameErr = $lnameErr = $phoneErr = $emailErr = $streetErr = $cityErr = $dobErr = $passwordErr = "";
$fname = $lname = $phone = $email = $street = $city = $dob = $password = "";
if ($_SERVER['REQUEST_METHOD'] == "POST"){
if (empty($_POST['Firstname'])){
$fnameErr = "First name is required";
// echo $fnameErr;
}
else{
$fname = test_input($_POST['Firstname']);
if (!preg_match("/^[A-Z][a-z]*(['\-]{0,1}[A-Z][a-z]+)*$/",$fname)){
$fnameErr = "First name should start with a capital letter and consists of alphabets only.";
//echo $fnameErr;
}
}
if (empty($_POST['Lastname'])){
$lnameErr = "Last name is required";
// echo $lnameErr;
}
else{
$lname = test_input($_POST['Lastname']);
if (!preg_match("/^[A-Z][a-z]*(['\-]{0,1}[A-Z][a-z]+)*$/",$lname)){
$lnameErr = "Last name should start with a capital letter and consists of alphabets only.";
//echo $lnameErr;
}
}
if (empty($_POST['Phone'])){
$phoneErr = "Phone number is required.";
//echo $phoneErr;
}
else{
$phone = test_input($_POST['Phone']);
if (!preg_match("/^(5[24789]([0-9]{6}))|([2469]([0-9]{6}))$/",$phone)){
$phoneErr = "Wrong format for phone number";
//echo $phoneErr;
}
}
if (empty($_POST['Email'])){
$emailErr = "Email is required";
//echo $emailErr;
}
else {
$email = test_input($_POST['Email']);
if (!preg_match("/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/",$email)){
$emailErr = "Wrong format for email";
//echo $emailErr;
}
}
if (empty($_POST['Street'])){
$streetErr = "Street name is required";
//echo $streetErr;
}
else{
$street = test_input($_POST['Street']);
if (!preg_match("/^[A-Z][a-z]+([\'\-\s](([A-Z][a-z]+)|[a-z]+))*$/",$street)){
$streetErr = "Street name should start with a capital letter and consists of alphabets only.";
//echo $streetErr;
}
}
if (empty($_POST['City'])){
$cityErr = "City name is required";
//echo $cityErr;
}
else{
$city = test_input($_POST['City']);
if (!preg_match("/^[A-Z][a-z]+([\-\s][A-Z][a-z]+)*$/",$city)){
$cityErr = "City name should start with a capital letter and consists of alphabets only.";
//echo $cityErr;
}
}
if (empty($_POST['DOB'])){
$dobErr = "Date of birth is required";
//echo $dobErr;
}
else {
$dob = test_input($_POST['DOB']);
$today = date("Y-m-d");
// echo $dob."<br>";
// echo $today."<br>";
if ($dob > $today){
$dobErr = "Invalid Date of birth";
//echo $dobErr;
}
}
if (empty($_POST["Password"])) {
$passwordErr = "Password is required";
//echo $passwordErr;
}
else {
$password = $_POST["Password"];
if (!preg_match("/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/",$password)){
$passwordErr = "Wrong password format";
//echo $passwordErr;
}
}
if ($fnameErr =="" && $lnameErr =="" && $phoneErr =="" && $emailErr =="" && $streetErr =="" && $cityErr =="" && $dobErr =="" && $passwordErr == ""){
require_once "includes/database.php";
$msg = "";
$hashed_password = password_hash($password,PASSWORD_DEFAULT);
$SQL= "Select NOW();";
$result=$conn->query($SQL);
$now= $result->fetch()["NOW()"];
$email = strtolower($email);
$sInsert = "INSERT INTO barber (first_name,last_name,phone,dob,email,password,last_active,street,city) VALUES ("
.$conn->quote($fname).","
.$conn->quote($lname).","
.$conn->quote($phone).","
.$conn->quote($dob).","
.$conn->quote($email).","
.$conn->quote($hashed_password).","
.$conn->quote($now).","
.$conn->quote($street).","
.$conn->quote($city).")";
$result = $conn->exec($sInsert);
if ($result){
$msg = "Record saved";
// echo $msg;
// header("Location: create-barber.php");
// header("Location: index.php");
// die();
}
else{
$msg = "ERROR: Your credentials could not be saved!";
echo $msg;
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER['REQUEST_METHOD'] == "POST" || $fnameErr != "" || $lnameErr != "" || $phoneErr != "" || $emailErr != "" || $streetErr != "" || $cityErr != "" || $dobErr != "" || $passwordErr != ""){
$fname = $lname = $phone = $email = $street = $city = $dob = $password = "";
}
?>
<main class="content">
<div class="container-fluid p-0">
<div class="mb-3">
<h1 class="h3 d-inline align-middle">Create Barber Account</h1>
</div>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<div class="row">
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Barber Name
<?php if ($fnameErr != ""){ echo "<p class = 'error'>$fnameErr</p>";}?>
<?php if ($lnameErr != ""){ echo "<p class = 'error'>$lnameErr</p>";}?>
</h5>
</div>
<div class="card-body">
<input type="text" class="form-control" placeholder="Last name" name="Lastname" value = "<?php if ($lnameErr == ""){echo $lname;} ?>" title="Lastname should consists of letters and should start with a capital letter" pattern="[A-Z][a-z]*(['\-]{0,1}[A-Z][a-z]+)*$" required>
</div>
<div class="card-body">
<input type="text" class="form-control" placeholder="First name" name="Firstname" value = "<?php if ($fnameErr == ""){echo $fname;} ?>" title="Firstname should consists of letters and should start with a capital letter" pattern="[A-Z][a-z]*(['\-]{0,1}[A-Z][a-z]+)*$" required>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Barber Phone Number
<?php if ($phoneErr != ""){ echo "<p class = 'error'>$phoneErr</p>";}?>
</h5>
</div>
<div class="card-body">
<input type="tel" class="form-control" placeholder="Phone Number" name="Phone" value = "<?php if ($phoneErr == ""){echo $phone;} ?>" title ="Enter your phone number (Local one only)" pattern="(5[24789]([0-9]{6}))|([2469]([0-9]{6}))$" required>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Barber Email
<?php if ($emailErr != ""){ echo "<p class = 'error'>$emailErr</p>";}?>
</h5>
</div>
<div class="card-body">
<input type="email" class="form-control" placeholder="Email" name="Email" value = "<?php if ($emailErr == ""){echo $email;} ?>" title ="Enter your email ([email protected])" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required>
</div>
</div>
</div>
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Barber Address
<?php
if ($streetErr != ""){
echo "<p class = 'error'>$streetErr</p>";}
echo $cityErr;
if ($cityErr != ""){ echo "<p class = 'error'>$cityErr</p>";}
?>
</h5>
</div>
<div class="card-body">
<input type="text" class="form-control" placeholder="Street" name="Street" value="<?php if ($streetErr==""){echo $street;}?>" title="Enter a valid Street name" pattern="[A-Z][a-z]+([\'\-\s](([A-Z][a-z]+)|[a-z]+))*$" required><br><br>
<input type="text" class="form-control" placeholder="City" name="City" value="<?php if ($cityErr==""){echo $city;}?>" title="Enter a valid City name" pattern="[A-Z][a-z]+([\-\s][A-Z][a-z]+)*$" required>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Barber Password
<?php if ($passwordErr != ""){ echo "<p class = 'error'>$passwordErr</p>";}?>
</h5>
</div>
<div class="card-body">
<input type="password" class="form-control" placeholder="Password" name="Password" value = "<?php if ($passwordErr == ""){echo $password;} ?>" title ="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$" required>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Barber Date of Birth
<?php if ($dobErr != ""){ echo "<p class = 'error'>$dobErr</p>";}?>
</h5>
</div>
<div class="card-body">
<input type="text" class="form-control" placeholder="Date of Birth" onfocus="(this.type='date')" name="DOB" value = "<?php if ($dobErr == ""){echo $dob;} ?>" title ="Enter your date of birth (dd-mm-yyyy)" required>
</div>
</div>
</div>
</div>
<input type="submit" value="Ok" class="form-control ok-btn" style="width: 10%;">
</form>
</div>
</main>
</div><!--end of main div-->
</div><!--end of wrapper div-->
<?php //} ?>
</body>
</html>