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

// Start session to access session variables
session_start();

// Check if user is logged in
if (!isset($_SESSION['student_id'])) {
    die('Access denied. Please log in.');
}

// Get the request ID from the URL parameter
if (isset($_GET['request_id']) && is_numeric($_GET['request_id'])) {
    $request_id = intval($_GET['request_id']);
    
    // Get the current student's ID from session
    $student_id = $_SESSION['student_id'];

    // Prepare and execute the SQL query to delete the request
    $query = "DELETE FROM student_requests WHERE request_id = ? AND student_id = ? AND status = 'Pending'";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("ii", $request_id, $student_id);

    if ($stmt->execute()) {
        // Request successfully deleted
        header("Location: sent_requests.php?status=success");
    } else {
        // Error handling
        header("Location: sent_requests.php?status=error");
    }
    $stmt->close();
} else {
    // Invalid request ID
    header("Location: sent_requests.php?status=error");
}

// Close the database connection
$conn->close();
?>
