The Class Management system allows teachers to create multiple classes, enroll students, and manage class rosters. Teachers can add students individually, import bulk enrollments from Excel files, and export class rosters for record-keeping.
case 'create_class': $class_name = trim($_POST['class_name']); $stmt = $pdo->prepare("INSERT INTO classes (teacher_id, name) VALUES (?, ?)"); $stmt->execute([$teacher_id, $class_name]); $_SESSION['success'] = "Class created successfully"; break;
Class names should be descriptive and unique within your own classes. Common naming conventions include course code, subject, or period (e.g., “CS101”, “Math Period 1”).
Students can be added to classes individually by entering their student ID. The system validates the student exists and enforces the one-class-per-teacher rule.
function addStudent($pdo, $class_id, $student_id, $teacher_id) { // Verify class belongs to teacher $stmt = $pdo->prepare("SELECT id FROM classes WHERE id = ? AND teacher_id = ?"); $stmt->execute([$class_id, $teacher_id]); if ($stmt->rowCount() === 0) { $_SESSION['error'] = "Invalid class selected"; return; } // Check if student exists $stmt = $pdo->prepare("SELECT * FROM students WHERE student_id = ?"); $stmt->execute([$student_id]); if ($stmt->rowCount() > 0) { // Check if student is already in one of THIS teacher's classes $stmt = $pdo->prepare(" SELECT cs.class_id, c.name FROM class_students cs JOIN classes c ON cs.class_id = c.id WHERE cs.student_id = ? AND c.teacher_id = ? "); $stmt->execute([$student_id, $teacher_id]); $existing_enrollment = $stmt->fetch(); if ($existing_enrollment) { if ($existing_enrollment['class_id'] == $class_id) { $_SESSION['error'] = "Student is already enrolled in this class"; } else { $_SESSION['error'] = "Student is already enrolled in your class: " . $existing_enrollment['name'] . ". A student can only be enrolled in one class per teacher."; } } else { $stmt = $pdo->prepare("INSERT INTO class_students (class_id, student_id) VALUES (?, ?)"); $stmt->execute([$class_id, $student_id]); $_SESSION['success'] = "Student added successfully"; } } else { $_SESSION['error'] = "Student ID not found"; }}
Class Ownership - Verify the class belongs to the current teacher
Student Exists - Confirm the student ID is registered in the system
No Duplicates - Prevent adding the same student twice to the same class
One Class Per Teacher - Enforce the rule that students can only be in one class per teacher
Important Rule: A student can be enrolled in only ONE class per teacher. However, students can be enrolled in classes from different teachers simultaneously.
// Get students in the class$stmt = $pdo->prepare(" SELECT s.student_id, s.name, s.email FROM students s JOIN class_students cs ON s.student_id = cs.student_id WHERE cs.class_id = ? ORDER BY s.name");$stmt->execute([$class_id]);$students = $stmt->fetchAll();// Create spreadsheet$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();$sheet = $spreadsheet->getActiveSheet();// Add headers$sheet->setCellValue('A1', 'Student ID');$sheet->setCellValue('B1', 'Name');$sheet->setCellValue('C1', 'Email');// Add data$row = 2;foreach ($students as $student) { $sheet->setCellValue('A' . $row, $student['student_id']); $sheet->setCellValue('B' . $row, $student['name']); $sheet->setCellValue('C' . $row, $student['email']); $row++;}
function deleteClass($pdo, $class_id, $teacher_id) { // Begin transaction to ensure all operations succeed or fail together $pdo->beginTransaction(); try { // Verify ownership $stmt = $pdo->prepare("SELECT * FROM classes WHERE id = ? AND teacher_id = ?"); $stmt->execute([$class_id, $teacher_id]); if ($stmt->rowCount() > 0) { // Delete attendance records for this class $stmt = $pdo->prepare("DELETE FROM attendance WHERE class_id = ?"); $stmt->execute([$class_id]); // Delete schedules for this class $stmt = $pdo->prepare("DELETE FROM class_schedules WHERE class_id = ?"); $stmt->execute([$class_id]); // Delete students from class $stmt = $pdo->prepare("DELETE FROM class_students WHERE class_id = ?"); $stmt->execute([$class_id]); // Delete class $stmt = $pdo->prepare("DELETE FROM classes WHERE id = ?"); $stmt->execute([$class_id]); $pdo->commit(); $_SESSION['success'] = "Class deleted successfully"; } else { $pdo->rollBack(); $_SESSION['error'] = "You don't have permission to delete this class"; } } catch (Exception $e) { $pdo->rollBack(); $_SESSION['error'] = "Error deleting class: " . $e->getMessage(); }}
The system enforces a business rule where each student can only be enrolled in ONE class per teacher, though they can be in multiple classes from different teachers.
<div class="info-banner"> <strong>Note:</strong> Students can be enrolled in multiple classes from different teachers, but only in one class per teacher.</div>