-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolunteerRegistration.php
285 lines (243 loc) · 10.4 KB
/
volunteerRegistration.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!--
PHP file
Author: Salma Emjaheed
Date: 12/14/2023
Description:
-->
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Volunteer Registration</title>
<link rel="stylesheet" href="finalProject.css" />
</head>
<body>
<h1>Volunteer Registration</h1>
<p>
Thank you for your willingness to volunteer to help Salma's Extremely Worthy Cause.
Please fill out the form to sign up fo a shift. You may register for more than one slot.
Duplicte registrations (for the same slot) will not be processed.
</p>
<?php
// establish a connection to MySQL database
$hn = "localhost";
$un = "mcuser";
$pw = "Pa55word";
$conn = new mysqli($hn, $un, $pw);
if ($conn->connect_error) die("Fatal Error"); // if the there is an error during the connection to the database
// create the form database if it does not already exist
$query = "CREATE DATABASE IF NOT EXISTS form";
$result = $conn->query($query);
if (!$result) die("Fatal Error");
// use form database
$query = "USE form";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
// create volunteers table
$query = "CREATE TABLE IF NOT EXISTS volunteers (
volunteerId INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(75),
lastname VARCHAR(75),
email VARCHAR(126),
PRIMARY KEY (volunteerId))";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
// create shift table
$query = "CREATE TABLE IF NOT EXISTS shift (
shiftId INT AUTO_INCREMENT,
startTime VARCHAR(126),
endTime VARCHAR(126),
PRIMARY KEY (shiftId))";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
// create volunteer_shift table
$query = "CREATE TABLE IF NOT EXISTS volunteer_shift (
shiftId INT AUTO_INCREMENT,
volunteerId INT,
FOREIGN KEY (volunteerId) REFERENCES volunteers (volunteerId),
FOREIGN KEY (shiftId) REFERENCES shift (shiftId),
PRIMARY KEY (shiftId, volunteerId))";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
$errFirstName = "";
$errLastName = "";
$first = "";
$last = "";
$email = "";
$shift = "";
$displayMessage = "";
$remainingCount = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$first = $_POST['fname'];
$last = $_POST['lname'];
$email = $_POST['email'];
$shift = $_POST['shift'];
$errorsFName = array();
$errorsLName = array();
$remainingCount = array();
// validate first name
if (!preg_match('/^[a-zA-Z ]+$/', $first)) {
$errorsFName[] = "Please enter letters and spaces only.";
}
else if (!preg_match('/[A-Z]/', $first) && !preg_match('/[a-z]/', $first)) {
$errorsFName[] = "First name must include at least one letter.";
}
// Display last name error message
if (!empty($errorsFName)) {
// Display error message
foreach ($errorsFName as $error) {
$errFirstName = $error;
}
}
// validate last name
if (!preg_match('/^[a-zA-Z ]+$/', $last)) {
$errorsLName[] = "Please enter letters and spaces only.";
}
else if (!preg_match('/[A-Z]/', $last) && !preg_match('/[a-z]/', $last)) {
$errorsLName[] = "Last name must include at least one letter.";
}
// Display last name error message
if (!empty($errorsLName)) {
// Display error message
foreach ($errorsLName as $error) {
$errLastName = $error;
}
}
$delimiter = "-";
function cutStringInHalf($shift, $delimiter)
{
// Explode the string into an array based on the delimiter
$parts = explode($delimiter, $shift);
// Assuming the array has at least two elements (start and end time)
if (count($parts) >= 2) {
return [
'startTime' => trim($parts[0]), // Trim to remove any leading/trailing whitespaces
'endTime' => trim($parts[1]),
];
} else {
// Handle the case where the string doesn't have enough parts
return null;
}
}
// Function to update remaining count
function updateRemainingCount($conn, $shift) {
// Get the count of registrations for the specific shift
$time = cutStringInHalf($shift, $delimiter);
$conn = new mysqli($hn, $un, $pw);
$queryCount = "SELECT COUNT(*) AS count FROM shift WHERE startTime='{$time['startTime']}'";
$resultCount = $conn->query($queryCount);
if (!$resultCount) die("Fatal Error: " . mysqli_error($conn));
if ($resultCount) {
$row = $resultCount->fetch_assoc();
$count = $row['count'];
// Calculate the remaining count
$remainingCount[$shift] = 5 - $count;
// update the counts in the session
$_SESSION['remainingCount'][$shift] = $remainingCount[$shift];
// Save the updated counts in the session
return $remainingCount;
}
}
// reflect the information in the database
$shifts = ["6:00pm - 7:00pm", "7:00pm - 8:00pm", "8:00pm - 9:00pm", "9:00pm - 10:00pm", "10:00pm - 11:00pm"];
foreach ($shifts as $shiftOption) {
updateRemainingCount($conn, $shiftOption);
}
if (empty($errorsFName) && empty($errorsLName)) {
$time = cutStringInHalf($shift, "-");
// Check for duplicate registration
$queryCheckDuplicate = "SELECT EXISTS (
SELECT 1 FROM volunteer_shift
JOIN shift ON volunteer_shift.shiftId = shift.shiftId
JOIN volunteers ON volunteer_shift.volunteerId = volunteers.volunteerId
WHERE volunteers.firstname = '$first' AND volunteers.lastname = '$last' AND volunteers.email = '$email'
AND shift.startTime = '{$time['startTime']}' AND shift.endTime = '{$time['endTime']}'
) AS isDuplicate";
$resultCheckDuplicate = $conn->query($queryCheckDuplicate);
if (!$resultCheckDuplicate) die("Fatal Error: " . mysqli_error($conn));
$querySameVolunteer = "SELECT * FROM volunteers WHERE firstname='$first' AND lastname='$last' AND email = '$email'";
$resultSameVolunteer = $conn->query($querySameVolunteer);
$querySameShift = "SELECT * FROM shift WHERE startTime='{$time['startTime']}'";
$resultSameShift = $conn->query($querySameVolunteer);
if ($resultCheckDuplicate->num_rows == 0) { // displayMessage is not displaying but everything else is working fine.
$displayMessage = "<b>Duplicate registration not processed. Thanks, " . "$first!<b>";
} else {
// Your existing code for valid registration...
if ($resultSameVolunteer->num_rows > 0) {
$query = "SELECT volunteerID FROM volunteers WHERE firstname='$first' AND lastname='$last' AND email = '$email'";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
$volunteerID = $conn->insert_id;
// the same volunteer will have to go for another shift, so always insert new shift
$query = "INSERT INTO shift (startTime, endTime) VALUES ('{$time['startTime']}', '{$time['startTime']}')";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
$shiftId = $conn->insert_id;
$query = "INSERT INTO volunteer_shift (volunteerID, shiftId) VALUES ($volunteerID, $shiftId)";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
$displayMessage = "<b>Thank you for being so generous with your time, " . "$first!<b>";
} else {
$query = "INSERT INTO volunteers (firstname, lastname, email) VALUES ('$first', '$last', '$email')";
$result = $conn->query($query);
if (!$result) die("Fatal Error");
$volunteerID = $conn->insert_id;
if ($resultSameShift->num_rows > 0) {
$query = "SELECT shiftId FROM shift WHERE startTime='{$time['startTime']}'";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
} else {
$query = "INSERT INTO shift (startTime, endTime) VALUES ('{$time['startTime']}', '{$time['startTime']}')";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
}
$shiftId = $conn->insert_id;
$query = "INSERT INTO volunteer_shift (volunteerID, shiftId) VALUES ($volunteerID, $shiftId)";
$result = $conn->query($query);
if (!$result) die("Fatal Error: " . mysqli_error($conn));
$displayMessage = "<b>Thank you for registering to volunteer, " . "$first!<b>";
}
}
// clear firs and last name, email, and shift if form is submitted and there are no errors
$first = "";
$last = "";
$email = "";
$shift = "";
}
}
?>
<form action="volunteerRegistration.php" method="post">
<label for="fname" class="text">First Name</label>
<input type="text" name="fname" value="<?php echo $first; ?>" required />
<p class="errFirstName"><?php echo $errFirstName?></p>
<label for="lname" class="text">Last Name</label>
<input type="text" name="lname" value="<?php echo $last; ?>" required />
<p class="errLastName"><?php echo $errLastName?></p>
<label for="email" class="text">Email</label>
<input type="email" name="email" value="<?php echo $email; ?>" required />
<label for="shift">Volunteer Shift</label>
<select id="shift" name="shift" size="5" required >
<?php
$shifts = ["6:00pm - 7:00pm", "7:00pm - 8:00pm", "8:00pm - 9:00pm", "9:00pm - 10:00pm", "10:00pm - 11:00pm"];
foreach ($shifts as $shiftOption) {
$remainingCount = updateRemainingCount($conn, $shiftOption);
// Check if the remaining count for the shift is greater than 0
if (isset($remainingCount[$shiftOption]) && $remainingCount[$shiftOption] > 0) {
// Display the option with the updated string
echo "<option value=\"$shiftOption\">$shiftOption ($remainingCount[$shiftOption] of 5 slots open)</option>";
} else {
// If count is 0, disable the option
echo "<option value=\"$shiftOption\" disabled>$shiftOption: (0 of 5 slots open)</option>";
}
}
// close connection
$conn->close();
?>
</select>
<input type="submit" value="Register" />
</form>
<p id="displayMessage"><?php echo $displayMessage?></p>
</body>
</html>