<?php
// Include your database connection
include('db_connection.php');

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate and sanitize inputs
    $teacher_id = $_POST['teacher'];
    $date = $_POST['date'];
    $status = $_POST['status'];

    // Prepare SQL statement to insert attendance record
    $query = "INSERT INTO attendance (teacher_id, attendance_date, status) VALUES (?, ?, ?)";
    $stmt = mysqli_prepare($conn, $query);

    // Bind parameters and execute statement
    mysqli_stmt_bind_param($stmt, "iss", $teacher_id, $date, $status);
    $success = mysqli_stmt_execute($stmt);

    // Check if insertion was successful
    if ($success) {
        echo "Attendance marked successfully!";
    } else {
        echo "Error marking attendance: " . mysqli_error($conn);
    }

    // Close statement and connection
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
} else {
    // Redirect back to mark_attendance.php if accessed directly
    header("Location: mark_attendance.php");
    exit();
}
?>
