<?php
include("includes/db.php");

if (isset($_POST["studentId"]) && isset($_POST["classId"]) && isset($_POST["examId"])) {
    $studentId = $conn->real_escape_string($_POST["studentId"]);
    $classId = $conn->real_escape_string($_POST["classId"]);
    $examId = $conn->real_escape_string($_POST["examId"]);

    // Fetch marks for the selected student, class, and exam
    $query = "SELECT subject_id, marks 
              FROM marks 
              WHERE student_id = ? AND class_id = ? AND exam_type = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("iii", $studentId, $classId, $examId);
    $stmt->execute();
    $result = $stmt->get_result();

    $marks = [];
    while ($row = $result->fetch_assoc()) {
        $marks[$row['subject_id']] = $row['marks'];
    }

    // Return marks as JSON
    echo json_encode($marks);
}
?>
