<?php
// Check if attendance_id is provided and valid
if (!isset($_GET['attendance_id']) || !is_numeric($_GET['attendance_id'])) {
    // Handle error, e.g., redirect back to manage_attendance.php with a message
    header("Location: manage_attendance.php?error=Invalid attendance ID");
    exit;
}

$attendance_id = $_GET['attendance_id'];

// Connect to the database (assuming db_connection.php includes your connection code)
include('db_connection.php');

// Prepare DELETE statement
$delete_query = "DELETE FROM attendance WHERE attendance_id = ?";
$stmt = $conn->prepare($delete_query);

if (!$stmt) {
    // Handle error
    die('Error preparing statement: ' . $conn->error);
}

// Bind parameters and execute
$stmt->bind_param("i", $attendance_id);
$stmt->execute();

// Check if deletion was successful
if ($stmt->affected_rows > 0) {
    // Redirect to manage_attendance.php with success message
    header("Location: manage_attendance.php?success=Attendance deleted successfully");
} else {
    // Redirect to manage_attendance.php with error message
    header("Location: manage_attendance.php?error=Failed to delete attendance");
}

// Close statement and database connection
$stmt->close();
$conn->close();
?>
