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

// Check if the teacher is logged in
if (!isset($_SESSION['teacher_id'])) {
    header("Location: teacher_login.php");
    exit();
}

// Get form data
$attendance_id = isset($_POST['attendance_id']) ? intval($_POST['attendance_id']) : 0;
$student_id = isset($_POST['student_id']) ? intval($_POST['student_id']) : 0;
$class_id = isset($_POST['class_id']) ? intval($_POST['class_id']) : 0;
$date = isset($_POST['date']) ? $_POST['date'] : '';
$status = isset($_POST['status']) ? $_POST['status'] : '';

// Validate form data
if ($attendance_id > 0 && $student_id > 0 && $class_id > 0 && !empty($date) && !empty($status)) {
    // Update existing attendance record
    $sql = "UPDATE student_attendance SET date = ?, status = ? WHERE attendance_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssi", $date, $status, $attendance_id);
    if ($stmt->execute()) {
        header("Location: attendance.php");
        exit();
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $stmt->close();
} else {
    echo "Invalid data provided.";
}

$conn->close();
?>
