<?php
session_start();
include('db_connection.php');

if (!isset($_SESSION['teacher_id'])) {
    header("Location: teacher_login.php");
    exit();
}

$class_id = $_POST['class_id'];
$date = $_POST['date'];
$attendance = $_POST['attendance'];

$sql = "INSERT INTO student_attendance (student_id, class_id, date, status)
        VALUES (?, ?, ?, ?)";

$stmt = $conn->prepare($sql);

foreach ($attendance as $student_id => $status) {
    $stmt->bind_param("iiss", $student_id, $class_id, $date, $status);
    $stmt->execute();
}

$stmt->close();
$conn->close();

header("Location: attendance.php");
exit();
?>