-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedit_stdnt.php
More file actions
287 lines (258 loc) · 12.2 KB
/
Copy pathedit_stdnt.php
File metadata and controls
287 lines (258 loc) · 12.2 KB
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
<?php
ob_start();
include("partials/header.php");
?>
<?php
// Include database connection
// include("config/db_connect.php");
if (isset($_POST['updatestdnt'])) {
$student_id = intval($_POST['student_id']);
$firstName = strtoupper(trim(mysqli_real_escape_string($conn, $_POST['first_name'])));
$lastName = strtoupper(trim(mysqli_real_escape_string($conn, $_POST['last_name'])));
$gender = strtoupper(mysqli_real_escape_string($conn, $_POST['gender']));
$dob = mysqli_real_escape_string($conn, $_POST['dob']);
$status = strtoupper(mysqli_real_escape_string($conn, $_POST['status']));
$lin = strtoupper(trim(mysqli_real_escape_string($conn, $_POST['lin'])));
$classID = intval($_POST['stdnt_class']);
$streamID = strtoupper(intval($_POST['stdnt_stream']));
// Array to store validation errors
$errors = [];
// ✅ VALIDATIONS
if (!preg_match("/^[a-zA-Z]+$/", $firstName)) {
$errors[] = "First name must contain only letters.";
}
if (!preg_match("/^[a-zA-Z ]+$/", $lastName)) {
$errors[] = "Last name must contain only letters.";
}
if (empty($gender)) {
$errors[] = "Please select gender.";
}
if (empty($dob)) {
$errors[] = "Date of birth is required.";
}
if (empty($status)) {
$errors[] = "Please select status.";
}
if (empty($classID)) {
$errors[] = "Please select class.";
}
if (empty($streamID)) {
$errors[] = "Please select stream.";
}
// Check for duplicate (ignore current student)
$checkDuplicate = "SELECT * FROM students
WHERE first_name='$firstName'
AND last_name='$lastName'
AND student_id != $student_id";
$runDuplicate = mysqli_query($conn, $checkDuplicate);
if (mysqli_num_rows($runDuplicate) > 0) {
$errors[] = "A student with the same name already exists.";
}
// If errors exist, display them
if (!empty($errors)) {
foreach ($errors as $err) {
echo "<div class='alert alert-danger'>$err</div>";
}
} else {
// ✅ HANDLE IMAGE UPLOAD
$image_name = ""; // Will hold final image name
$selectOldImg = mysqli_query($conn, "SELECT image FROM students WHERE student_id=$student_id");
$oldData = mysqli_fetch_assoc($selectOldImg);
$old_image = $oldData['image'];
if (!empty($_FILES['image']['name'])) {
$image_name = $_FILES['image']['name'];
$image_ext = pathinfo($image_name, PATHINFO_EXTENSION);
$allowed_ext = ['jpg', 'jpeg', 'png'];
if (in_array(strtolower($image_ext), $allowed_ext)) {
$new_image_name = "student_" . time() . "." . $image_ext;
$destination = "img/stdent_image/" . $new_image_name;
if (move_uploaded_file($_FILES['image']['tmp_name'], $destination)) {
// Delete old image if exists
if (!empty($old_image) && file_exists("img/stdent_image/" . $old_image)) {
unlink("img/stdent_image/" . $old_image);
}
$image_name = $new_image_name;
} else {
echo "<div class='alert alert-danger'>Failed to upload image.</div>";
$image_name = $old_image; // fallback to old
}
} else {
echo "<div class='alert alert-danger'>Invalid image type. Only JPG, PNG, JPEG allowed.</div>";
$image_name = $old_image;
}
} else {
$image_name = $old_image; // No new image, keep old
}
// ✅ UPDATE STUDENT DATA
$updateQuery = "UPDATE students SET
first_name='$firstName',
last_name='$lastName',
gender='$gender',
dob='$dob',
status='$status',
LIN='$lin',
class_id='$classID',
stream_id='$streamID',
image='$image_name'
WHERE student_id=$student_id";
$executeUpdate = mysqli_query($conn, $updateQuery);
if ($executeUpdate) {
echo "<div class='alert alert-success'>Student updated successfully.</div>";
header('Location:'.SITEURL."students.php");
// Optionally redirect
// header("Location: students.php?success=1");
// exit;
} else {
echo "<div class='alert alert-danger'>Failed to update student: " . mysqli_error($conn) . "</div>";
}
}
}
?>
<?php
// Fetch student data before displaying form
if (isset($_GET['student_id'])) {
$student_id = intval($_GET['student_id']);
$selectStudent = "SELECT * FROM students WHERE student_id = $student_id";
$executeStudent = mysqli_query($conn, $selectStudent);
if ($executeStudent && mysqli_num_rows($executeStudent) > 0) {
$studentData = mysqli_fetch_assoc($executeStudent);
$firstName = $studentData['first_name'];
$lastName = $studentData['last_name'];
$gender = $studentData['gender'];
$dob = $studentData['dob'];
$status = $studentData['status'];
$lin = $studentData['LIN'];
$classID = $studentData['class_id'];
$streamID = $studentData['stream_id'];
$image_name = $studentData['image'];
} else {
die("Student not found.");
}
} else {
die("No student ID provided.");
}
?>
<div class="container-fluid">
<div class="mb-2">
<a href="<?php echo SITEURL ?>students.php" class="btn btn-outline-secondary btn-sm">
<i class="fas fa-arrow-left me-1"></i> Back to Students
</a>
</div>
<div class="row">
<h3 class="text-capitalize fs-6 text-dark py-2">edit students</h3>
<div class="col-12 col-lg-6">
<form method="POST" action="" enctype="multipart/form-data">
<!-- Hidden input for student ID -->
<input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
<div class="mb-3">
<label for="firstname" class="form-label text-capitalize fw-bold">first name</label>
<input type="text" class="form-control shadow-none" name="first_name" id="firstname"
value="<?php echo htmlspecialchars($firstName); ?>" placeholder="first name" autocomplete="off">
</div>
<div class="mb-3">
<label for="lastname" class="form-label text-capitalize fw-bold">last name</label>
<input type="text" class="form-control shadow-none" name="last_name" id="lastname"
value="<?php echo htmlspecialchars($lastName); ?>" placeholder="last name" autocomplete="off">
</div>
<div class="mb-3">
<label class="form-label text-capitalize fw-bold">gender</label>
<select class="form-select shadow-none" name="gender" required>
<option disabled>Choose gender</option>
<option value="MALE" <?php echo ($gender == "male") ? "selected" : ""; ?>>male</option>
<option value="FEMALE" <?php echo ($gender == "female") ? "selected" : ""; ?>>female</option>
</select>
</div>
<div class="mb-3">
<label for="dob" class="form-label text-capitalize fw-bold">date of birth</label>
<input type="date" value="<?php echo htmlspecialchars($dob); ?>" class="form-control shadow-none" name="dob"
id="dob" autocomplete="off">
</div>
<div class="mb-3">
<label class="form-label text-capitalize fw-bold">status</label>
<select class="form-select shadow-none" name="status">
<option disabled>Choose status</option>
<option value="day" <?php echo ($status == "day") ? "selected" : ""; ?>>day</option>
<option value="boarding" <?php echo ($status == "boarding") ? "selected" : ""; ?>>boarding</option>
</select>
</div>
</div>
<div class="col-12 col-lg-6">
<div class="mb-3">
<label for="lin" class="form-label text-capitalize fw-bold">LIN</label>
<input type="text" class="form-control shadow-none text-uppercase" name="lin"
value="<?php echo htmlspecialchars($lin); ?>" id="lin" placeholder="Enter lin" autocomplete="off">
</div>
<div class="mb-3">
<label class="form-label text-capitalize fw-bold">Student Class</label>
<select id="studentClass" class="form-select shadow-none" name="stdnt_class" required>
<option value="" disabled <?= !$classID ? 'selected' : ''; ?>>Choose class</option>
<?php
$selectClass = "SELECT * FROM classes";
$executeClass = mysqli_query($conn, $selectClass);
while ($fetchedClass = mysqli_fetch_assoc($executeClass)) {
$class_id = $fetchedClass['id'];
$className = $fetchedClass['class_name'];
echo "<option value='$class_id' " . (($classID == $class_id) ? 'selected' : '') . ">$className</option>";
}
?>
</select>
</div>
<div class="mb-3">
<label class="form-label text-capitalize fw-bold">Student Stream</label>
<select id="studentStream" class="form-select shadow-none" name="stdnt_stream" required>
<option value="" disabled <?= !$streamID ? 'selected' : ''; ?>>Choose stream</option>
<?php
// Preload streams only if a class is selected
if ($classID) {
$selectStream = "SELECT * FROM streams WHERE class_id=$classID";
$executeStream = mysqli_query($conn, $selectStream);
while ($fetchedStrm = mysqli_fetch_assoc($executeStream)) {
$stream_id = $fetchedStrm['id'];
$streamName = $fetchedStrm['stream_name'];
echo "<option value='$stream_id' " . (($streamID == $stream_id) ? 'selected' : '') . ">$streamName</option>";
}
}
?>
</select>
</div>
<!-- jQuery for AJAX -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
function loadStreams(classID, selectedStream = null){
if(!classID) return;
$.get("get_streams.php", { class_id: classID })
.done(function(data){
$("#studentStream").html(data);
if(selectedStream) $("#studentStream").val(selectedStream);
})
.fail(function(xhr){
console.error("Stream AJAX Error:", xhr.responseText);
});
}
// On class change → fetch streams
$("#studentClass").change(function(){
loadStreams($(this).val());
});
// Preload streams if a class is already selected
<?php if($classID): ?>
loadStreams(<?= intval($classID) ?>, <?= $streamID ? intval($streamID) : 'null' ?>);
<?php endif; ?>
});
</script>
<div class="mb-3">
<label for="image" class="form-label text-capitalize fw-bold">upload new image</label>
<input type="file" class="form-control shadow-none" name="image" id="image" accept=".jpg, .png, .jpeg">
<?php if (!empty($image_name)) : ?>
<small class="d-block mt-1">Current Image: <img class="img-fluid d-block" src="<?php echo SITEURL ?>img/stdent_image/<?php echo $image_name ?>" width="100" height="100" ></small>
<?php else: ?>
<?php echo "No Image" ?>
<?php endif; ?>
</div>
<button type="submit" name="updatestdnt" class="btn btn-warning text-capitalize">Update student</button>
</form>
</div>
</div>
</div>
</div>
<?php include("partials/footer.php") ?>