Skip to main content
Manage teacher accounts, profiles, and access to class management features.

Teacher Registration

Endpoint: register.php Method: POST Description: Creates a new teacher account with a unique teacher ID.

Request Parameters

user_type
string
required
Must be set to teacher
teacher_id
string
required
Teacher identifier in format T\d{4} (e.g., T1234, T5678)
name
string
required
Teacher’s full name
email
email
required
Valid email address for account management
password
string
required
Account password (minimum 8 characters)
confirm_password
string
required
Password confirmation (must match password)

Teacher ID Format

Pattern: ^T\d{4}$ Valid Examples:
  • T0001
  • T1234
  • T9999
Invalid Examples:
  • 1234 (missing ‘T’ prefix)
  • T123 (only 3 digits)
  • T12345 (too many digits)
  • t1234 (lowercase)

Validation Process

  1. Password Match:
    if ($password !== $confirm_password) {
        echo "Passwords do not match";
    }
    
  2. Teacher ID Format:
    if (!preg_match('/^T\d{4}$/', $teacher_id)) {
        echo "Teacher ID must be in format T followed by 4 digits (e.g., T1234)";
    }
    
  3. Uniqueness Check:
    SELECT COUNT(*) FROM teachers WHERE teacher_id = ?
    

Database Insertion

INSERT INTO teachers (teacher_id, name, email, password) 
VALUES (?, ?, ?, ?)
Password Hashing:
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

Response Messages

Success:
<div class="success-message">Teacher registration successful! You can now login.</div>
Errors:
  • “Passwords do not match”
  • “Teacher ID must be in format T followed by 4 digits (e.g., T1234)”
  • “Teacher ID already exists”
  • “Registration failed: [database error]”
Teacher IDs must be unique across the entire system. Each teacher must have a distinct ID starting with ‘T’.

Teacher Profile Management

Page: teacher_dashboard.php Authentication: Required (teacher session)

View Profile Information

Session Data:
$_SESSION['teacher_id']    // e.g., "T1234"
$_SESSION['teacher_name']  // e.g., "John Smith"
$_SESSION['user_type']     // "teacher"
Database Query:
SELECT email FROM teachers WHERE teacher_id = ?

Response Fields

teacher_id
string
Unique teacher identifier (format: T\d)
name
string
Teacher’s full name
email
string
Registered email address

Update Email Address

Email update functionality for teachers is currently not implemented. The teacher dashboard UI references update_email.php, but this file does not exist in the source code.
Intended Endpoint: update_email.php (not implemented) Method: POST Authentication: Teacher session required

Expected Implementation

To implement this feature, create update_email.php with the following functionality: Request Parameters:
new_email
email
required
New email address
password
string
required
Current password for verification
Suggested Validation Process:
  1. Password Verification:
    $stmt = $pdo->prepare("SELECT password FROM teachers WHERE teacher_id = ?");
    $stmt->execute([$teacher_id]);
    $teacher = $stmt->fetch();
    
    if (!password_verify($password, $teacher['password'])) {
        $_SESSION['error'] = "Incorrect password";
        header("Location: teacher_dashboard.php");
        exit();
    }
    
  2. Email Uniqueness Check:
    SELECT COUNT(*) FROM teachers 
    WHERE email = ? AND teacher_id != ?
    
  3. Update Database:
    UPDATE teachers SET email = ? WHERE teacher_id = ?
    
Students can update their email through student_dashboard.php (inline processing). Reference that implementation for consistency.

Change Password

Endpoint: update_password.php Method: POST Authentication: Teacher session required

Request Parameters

current_password
string
required
Current account password
new_password
string
required
New password (minimum 8 characters)
confirm_password
string
required
Must match new_password

Validation Steps

  1. Password Match:
    if ($new_password !== $confirm_password) {
        $_SESSION['error'] = "Passwords do not match";
    }
    
  2. Current Password Verification:
    $stmt = $pdo->prepare("SELECT password FROM teachers WHERE teacher_id = ?");
    $stmt->execute([$teacher_id]);
    $teacher = $stmt->fetch();
    
    if (!password_verify($current_password, $teacher['password'])) {
        $_SESSION['error'] = "Current password is incorrect";
    }
    
  3. Update Password:
    $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
    $stmt = $pdo->prepare("UPDATE teachers SET password = ? WHERE teacher_id = ?");
    $stmt->execute([$hashed_password, $teacher_id]);
    

Response Messages

Success:
<div class="message success">Password updated successfully</div>
Errors:
  • “Passwords do not match”
  • “Current password is incorrect”
  • “Password must be at least 8 characters long”
Password changes require verification of the current password to prevent unauthorized modifications.

View Teacher’s Classes

Query: Retrieve all classes owned by the teacher
SELECT * FROM classes 
WHERE teacher_id = ? 
ORDER BY name

Response Fields

id
integer
Unique class identifier
teacher_id
string
Owner teacher’s ID
name
string
Class name

Usage

Displayed in teacher dashboard for class selection:
<select class="class-select" onchange="window.location.href='?class_id=' + this.value">
    <option value="">Select a class</option>
    <?php foreach ($classes as $class): ?>
        <option value="<?= $class['id'] ?>">
            <?= $class['name'] ?>
        </option>
    <?php endforeach; ?>
</select>

Class Ownership Verification

Purpose: Ensure teachers can only access their own classes

Verification Query

SELECT id FROM classes 
WHERE id = ? AND teacher_id = ?
Parameters:
  • ? - Class ID being accessed
  • ? - Logged-in teacher’s ID

Implementation

if ($selected_class) {
    $verify_class = $pdo->prepare(
        "SELECT id FROM classes WHERE id = ? AND teacher_id = ?"
    );
    $verify_class->execute([$selected_class, $teacher_id]);
    
    if ($verify_class->rowCount() === 0) {
        $_SESSION['error'] = "You don't have permission to view this class";
        $selected_class = null;
    }
}
All class-related operations must verify ownership before allowing access or modifications.

View Class Statistics

Queries: Get attendance metrics for a specific class

Total Students in Class

SELECT COUNT(*) FROM class_students 
WHERE class_id = ?

Present Today

SELECT COUNT(DISTINCT a.student_id) 
FROM attendance a 
WHERE a.class_id = ? AND a.date = CURRENT_DATE()

Attendance Rate Calculation

$attendance_rate = $student_count > 0 
    ? round(($attendance_count / $student_count) * 100) 
    : 0;

Response Fields

student_count
integer
Total number of students enrolled in the class
attendance_count
integer
Number of students present today
attendance_rate
integer
Percentage of students present (0-100)

View Today’s Attendance

Query: Get attendance records for all students in class
SELECT 
    cs.student_id,
    s.name,
    a.id as attendance_id,
    a.time,
    COALESCE(a.status, 'absent') as status
FROM class_students cs
JOIN students s ON cs.student_id = s.student_id
LEFT JOIN attendance a ON cs.student_id = a.student_id 
    AND a.class_id = cs.class_id 
    AND a.date = CURRENT_DATE()
WHERE cs.class_id = ?
ORDER BY s.name

Response Fields

student_id
string
Student identifier
name
string
Student’s full name
attendance_id
integer
Attendance record ID (null if absent)
time
time
Check-in time (null if absent)
status
string
Attendance status: on-time, late, or absent

Update Attendance Status

Endpoint: teacher_dashboard.php Method: POST Description: Allows teachers to manually modify attendance status

Request Parameters

update_status
string
required
Action identifier (set to “1”)
attendance_id
integer
required
ID of the attendance record to update
new_status
string
required
New status: on-time, late, or absent

Verification Process

  1. Ownership Check:
    SELECT a.id 
    FROM attendance a
    JOIN classes c ON a.class_id = c.id
    WHERE a.id = ? AND c.teacher_id = ?
    
  2. Update Status:
    UPDATE attendance 
    SET status = ? 
    WHERE id = ?
    

Response Messages

Success:
$_SESSION['success'] = "Attendance status updated successfully";
Error:
$_SESSION['error'] = "You don't have permission to modify this attendance record";
Teachers can only modify attendance records for classes they own. The system verifies ownership before allowing any updates.

Build docs developers (and LLMs) love