beginTransaction(); // Check if user with this email already exists $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$teacherEmail]); if ($stmt->fetch()) { $error = 'A user with this email already exists.'; $pdo->rollBack(); } else { // Generate a random password $password = bin2hex(random_bytes(8)); $hashed_password = password_hash($password, PASSWORD_DEFAULT); // Create a user account for the teacher $stmt = $pdo->prepare("INSERT INTO users (username, password, email, school_id, role) VALUES (?, ?, ?, ?, 'teacher')"); $stmt->execute([$teacherEmail, $hashed_password, $teacherEmail, $school_id]); $user_id = $pdo->lastInsertId(); // Create the teacher $stmt = $pdo->prepare("INSERT INTO teachers (name, user_id, school_id, can_edit_workload) VALUES (?, ?, ?, ?)"); if ($stmt->execute([$teacherName, $user_id, $school_id, $can_edit_workload])) { $pdo->commit(); $message = 'Teacher "' . htmlspecialchars($teacherName) . '" created successfully! Their password is: ' . $password; } else { $error = 'Failed to create teacher.'; $pdo->rollBack(); } } } catch (PDOException $e) { if ($e->errorInfo[1] == 1062) { // Duplicate entry $error = 'Error: A teacher with this email already exists.'; } else { $error = 'Database error: ' . $e->getMessage(); } if ($pdo->inTransaction()) { $pdo->rollBack(); } } } } // Fetch all teachers to display $teachers = []; try { $pdo = db(); $stmt = $pdo->prepare("SELECT t.id, t.name, u.email, t.can_edit_workload, t.created_at FROM teachers t JOIN users u ON t.user_id = u.id WHERE t.school_id = ? ORDER BY t.created_at DESC"); $stmt->execute([$school_id]); $teachers = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } ?> Admin: Manage Teachers - Haki Schedule

Manage Teachers

Create a New Teacher
Existing Teachers

No teachers have been created yet. Use the form above to add the first one.

Name Email Can Edit Workload Created Actions
Edit