90 lines
3.8 KiB
PHP
90 lines
3.8 KiB
PHP
<?php
|
|
|
|
include_once '../db/config.php';
|
|
|
|
// Ensure the user is an admin
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'Admin') {
|
|
header("Location: ../auth/login.php");
|
|
exit();
|
|
}
|
|
|
|
$pdo = db();
|
|
$student_id = $_GET['id'] ?? null;
|
|
|
|
if (!$student_id) {
|
|
header("Location: students.php");
|
|
exit();
|
|
}
|
|
|
|
// Handle Form Submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = $_POST['name'];
|
|
$email = $_POST['email'];
|
|
$gender = $_POST['gender'];
|
|
$year = $_POST['year'];
|
|
$department = $_POST['department'];
|
|
|
|
$stmt = $pdo->prepare("UPDATE Users SET name = ?, email = ?, gender = ?, year = ?, department = ? WHERE id = ?");
|
|
$stmt->execute([$name, $email, $gender, $year, $department, $student_id]);
|
|
|
|
header("Location: students.php?success=1");
|
|
exit();
|
|
}
|
|
|
|
// Fetch student data
|
|
$stmt = $pdo->prepare("SELECT name, email, gender, year, department FROM Users WHERE id = ? AND role = 'Student'");
|
|
$stmt->execute([$student_id]);
|
|
$student = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$student) {
|
|
header("Location: students.php");
|
|
exit();
|
|
}
|
|
|
|
include_once '../includes/header.php';
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h4 class="mb-0">Edit Student Details</h4>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="edit_student.php?id=<?php echo $student_id; ?>" method="POST">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Full Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($student['name']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email Address</label>
|
|
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($student['email']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="gender" class="form-label">Gender</label>
|
|
<select class="form-select" id="gender" name="gender" required>
|
|
<option value="Male" <?php echo ($student['gender'] == 'Male') ? 'selected' : ''; ?>>Male</option>
|
|
<option value="Female" <?php echo ($student['gender'] == 'Female') ? 'selected' : ''; ?>>Female</option>
|
|
<option value="Other" <?php echo ($student['gender'] == 'Other') ? 'selected' : ''; ?>>Other</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="year" class="form-label">Year</label>
|
|
<input type="number" class="form-control" id="year" name="year" value="<?php echo htmlspecialchars($student['year']); ?>" min="1" max="5">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="department" class="form-label">Department</label>
|
|
<input type="text" class="form-control" id="department" name="department" value="<?php echo htmlspecialchars($student['department']); ?>">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Student</button>
|
|
<a href="students.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include_once '../includes/footer.php'; ?>
|