-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud_senior_high.php
293 lines (239 loc) · 11.9 KB
/
crud_senior_high.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
286
287
288
289
290
291
292
293
<?php
// Include database connection code
include 'database/db.php';
include 'phpqrcode/qrlib.php';
// Fetch data from SeniorHighStudents table
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$query = "SELECT shs.ID, shs.IdentificationNumber, shs.FirstName, shs.LastName, shs.Email, s.strand_name, g.grade_name, se.section_name
FROM seniorhighstudents shs
INNER JOIN strands s ON shs.StrandID = s.id
INNER JOIN grades g ON shs.GradeID = g.id
INNER JOIN sections se ON shs.SectionID = se.id";
$result = mysqli_query($conn, $query);
if ($result) {
$students = array();
while ($row = mysqli_fetch_assoc($result)) {
$students[] = $row;
}
echo json_encode(array("data" => $students));
http_response_code(200); // OK
} else {
echo json_encode(array("error" => "Failed to fetch senior high students"));
http_response_code(500); // Internal Server Error
}
}
// Add a new student to the database
elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['identificationNumber'])) {
// Retrieve form data
$identificationNumber = $_POST['identificationNumber'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$strandName = $_POST['strand'];
$gradeName = $_POST['grade'];
$sectionName = $_POST['section'];
// Get StrandID from strands table
$strandQuery = "SELECT id FROM strands WHERE strand_name = ?";
$strandStmt = $conn->prepare($strandQuery);
$strandStmt->bind_param("s", $strandName);
$strandStmt->execute();
$strandResult = $strandStmt->get_result();
if ($strandResult->num_rows > 0) {
$strandRow = $strandResult->fetch_assoc();
$strandID = $strandRow['id'];
// Get GradeID from grades table
$gradeQuery = "SELECT id FROM grades WHERE grade_name = ?";
$gradeStmt = $conn->prepare($gradeQuery);
$gradeStmt->bind_param("s", $gradeName);
$gradeStmt->execute();
$gradeResult = $gradeStmt->get_result();
if ($gradeResult->num_rows > 0) {
$gradeRow = $gradeResult->fetch_assoc();
$gradeID = $gradeRow['id'];
// Get SectionID from sections table
$sectionQuery = "SELECT id FROM sections WHERE section_name = ?";
$sectionStmt = $conn->prepare($sectionQuery);
$sectionStmt->bind_param("s", $sectionName);
$sectionStmt->execute();
$sectionResult = $sectionStmt->get_result();
if ($sectionResult->num_rows > 0) {
$sectionRow = $sectionResult->fetch_assoc();
$sectionID = $sectionRow['id'];
// Prepare INSERT statement
$stmt = $conn->prepare("INSERT INTO SeniorHighStudents (IdentificationNumber, FirstName, LastName, Email, StrandID, GradeID, SectionID) VALUES (?, ?, ?, ?, ?, ?, ?)");
// Bind parameters
$stmt->bind_param("ssssiii", $identificationNumber, $firstName, $lastName, $email, $strandID, $gradeID, $sectionID);
// Execute statement
if ($stmt->execute()) {
// After successfully adding the student
$data = $identificationNumber; // Or any unique data
$qrCodePath = 'qr_codes/' . $identificationNumber . '.png';
QRcode::png($data, $qrCodePath);
echo json_encode(array("status" => "success"));
http_response_code(200); // OK
} else {
echo json_encode(array("error" => "Failed to add student"));
http_response_code(500); // Internal Server Error
}
} else {
echo json_encode(array("error" => "Invalid Section"));
http_response_code(400); // Bad Request
}
} else {
echo json_encode(array("error" => "Invalid Grade"));
http_response_code(400); // Bad Request
}
} else {
echo json_encode(array("error" => "Invalid Strand"));
http_response_code(400); // Bad Request
}
}
elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['editSeniorHighId'])) {
$studentId = $_POST['editSeniorHighId'];
$newIdentificationNumber = $_POST['editIdentificationNumber'];
// Fetch the current identification number
$currentQuery = "SELECT IdentificationNumber FROM seniorhighstudents WHERE ID = ?";
$currentStmt = $conn->prepare($currentQuery);
$currentStmt->bind_param("i", $studentId);
$currentStmt->execute();
$result = $currentStmt->get_result();
$currentData = $result->fetch_assoc();
$currentIdentificationNumber = $currentData['IdentificationNumber'];
// Get StrandID from the Strands table based on StrandName
$queryStrand = "SELECT ID FROM strands WHERE strand_name = ?";
$stmtStrand = $conn->prepare($queryStrand);
$stmtStrand->bind_param("s", $_POST['editStrand']); // Use $_POST['editStrand']
$stmtStrand->execute();
$resultStrand = $stmtStrand->get_result();
if ($resultStrand->num_rows > 0) {
// Strand exists, get StrandID
$rowStrand = $resultStrand->fetch_assoc();
$strandId = $rowStrand['ID'];
// Get GradeID from the Grades table based on GradeName
$queryGrade = "SELECT ID FROM grades WHERE grade_name = ?";
$stmtGrade = $conn->prepare($queryGrade);
$stmtGrade->bind_param("s", $_POST['editGrade']); // Use $_POST['editGrade']
$stmtGrade->execute();
$resultGrade = $stmtGrade->get_result();
if ($resultGrade->num_rows > 0) {
// Grade exists, get GradeID
$rowGrade = $resultGrade->fetch_assoc();
$gradeId = $rowGrade['ID'];
// Get SectionID from the Sections table based on SectionName
$querySection = "SELECT ID FROM sections WHERE section_name = ?";
$stmtSection = $conn->prepare($querySection);
$stmtSection->bind_param("s", $_POST['editSection']); // Use $_POST['editSection']
$stmtSection->execute();
$resultSection = $stmtSection->get_result();
if ($resultSection->num_rows > 0) {
// Section exists, get SectionID
$rowSection = $resultSection->fetch_assoc();
$sectionId = $rowSection['ID'];
// Prepare UPDATE statement
$stmtUpdate = $conn->prepare("UPDATE seniorhighstudents SET IdentificationNumber=?, FirstName=?, LastName=?, Email=?, StrandID=?, GradeID=?, SectionID=? WHERE ID=?");
$stmtUpdate->bind_param("ssssiiii", $newIdentificationNumber, $_POST['editFirstName'], $_POST['editLastName'], $_POST['editEmail'], $strandId, $gradeId, $sectionId, $studentId);
// Execute statement
if ($stmtUpdate->execute()) {
// Delete the old QR code if the identification number has changed
if ($currentIdentificationNumber !== $newIdentificationNumber) {
$oldQrCodePath = 'qr_codes/' . $currentIdentificationNumber . '.png';
if (file_exists($oldQrCodePath)) {
unlink($oldQrCodePath); // Delete the old QR code file
}
// Generate a new QR code with the new identification number
$newQrCodePath = 'qr_codes/' . $newIdentificationNumber . '.png';
QRcode::png($newIdentificationNumber, $newQrCodePath);
}
echo json_encode(array("status" => "success"));
http_response_code(200); // OK
} else {
echo json_encode(array("error" => "Failed to update senior high student"));
http_response_code(500); // Internal Server Error
}
} else {
echo json_encode(array("error" => "Section does not exist"));
http_response_code(400); // Bad Request
}
} else {
echo json_encode(array("error" => "Grade does not exist"));
http_response_code(400); // Bad Request
}
} else {
echo json_encode(array("error" => "Strand does not exist"));
http_response_code(400); // Bad Request
}
}
// Delete student
elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['student_id'])) {
$student_id = $_POST['student_id'];
// Fetch the student's identification number before deletion
$fetchQuery = "SELECT IdentificationNumber FROM SeniorHighStudents WHERE ID = ?";
$fetchStmt = $conn->prepare($fetchQuery);
$fetchStmt->bind_param("i", $student_id);
$fetchStmt->execute();
$result = $fetchStmt->get_result();
if ($result->num_rows > 0) {
$student = $result->fetch_assoc();
$identificationNumber = $student['IdentificationNumber'];
// Proceed with the student deletion
$stmt = $conn->prepare("DELETE FROM SeniorHighStudents WHERE ID = ?");
$stmt->bind_param("i", $student_id);
if ($stmt->execute()) {
// After successful deletion, delete the QR code
$qrCodePath = 'qr_codes/' . $identificationNumber . '.png';
if (file_exists($qrCodePath)) {
unlink($qrCodePath); // Delete the QR code file
}
echo json_encode(array('status' => 'success', 'message' => 'Student deleted successfully'));
http_response_code(200); // OK
} else {
echo json_encode(array('error' => 'Failed to delete student'));
http_response_code(500); // Internal Server Error
}
} else {
echo json_encode(array('error' => 'Student not found'));
http_response_code(404); // Not Found
}
}
// Bulk delete students
elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['bulkDelete'])) {
$student_ids = $_POST['student_ids']; // Assume this is an array of student IDs
if (is_array($student_ids)) {
// Fetch the identification numbers for all students to be deleted
$placeholders = implode(',', array_fill(0, count($student_ids), '?'));
$idQuery = "SELECT IdentificationNumber FROM SeniorHighStudents WHERE ID IN ($placeholders)";
$idStmt = $conn->prepare($idQuery);
// Dynamically bind parameters for student IDs
$idStmt->bind_param(str_repeat('i', count($student_ids)), ...$student_ids);
$idStmt->execute();
$result = $idStmt->get_result();
$identificationNumbers = [];
while ($row = $result->fetch_assoc()) {
$identificationNumbers[] = $row['IdentificationNumber'];
}
$idStmt->close();
// Proceed with deleting the students from the database
$deleteStmt = $conn->prepare("DELETE FROM SeniorHighStudents WHERE ID IN ($placeholders)");
// Dynamically bind parameters for student IDs again
$deleteStmt->bind_param(str_repeat('i', count($student_ids)), ...$student_ids);
if ($deleteStmt->execute()) {
// Delete QR codes for each student
foreach ($identificationNumbers as $idNum) {
$qrCodePath = 'qr_codes/' . $idNum . '.png';
if (file_exists($qrCodePath)) {
unlink($qrCodePath); // Delete the QR code file
}
}
echo json_encode(array('status' => 'success', 'message' => 'Students and their QR codes deleted successfully'));
http_response_code(200); // OK
} else {
echo json_encode(array('error' => 'Failed to delete students'));
http_response_code(500); // Internal Server Error
}
$deleteStmt->close();
} else {
echo json_encode(array('error' => 'Invalid student IDs'));
http_response_code(400); // Bad Request
}
}
?>