prepare("SELECT * FROM RoomRequests WHERE student_id = ? AND (status = 'pending' OR status = 'approved')"); $stmt->execute([$student_id]); $existing_request = $stmt->fetch(); // Check if student is already allocated a room $stmt = $pdo->prepare("SELECT * FROM Allocations WHERE student_id = ?"); $stmt->execute([$student_id]); $allocation = $stmt->fetch(); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['request_room']) && !$existing_request && !$allocation) { $room_id = $_POST['room_id']; $stmt = $pdo->prepare("INSERT INTO RoomRequests (student_id, room_id) VALUES (?, ?)"); $stmt->execute([$student_id, $room_id]); header('Location: request_room.php'); exit; } // Fetch available rooms (not occupied) $stmt = $pdo->query("SELECT * FROM Rooms WHERE id NOT IN (SELECT room_id FROM Allocations)"); $available_rooms = $stmt->fetchAll(); ?>