<?php
// Include the database configuration file
include("includes/db.php");

// Check if marksData, examId, classId, and subjectId are set and not empty
if (isset($_POST['marksData']) && !empty($_POST['marksData']) &&
    isset($_POST['examId']) && !empty($_POST['examId']) &&
    isset($_POST['classId']) && !empty($_POST['classId']) &&
    isset($_POST['subjectId']) && !empty($_POST['subjectId'])) {
    
    // Retrieve marks data, exam ID, class ID, and subject ID from the POST request
    $marksData = $_POST['marksData'];
    $examId = $_POST['examId'];
    $classId = $_POST['classId'];
    $subjectId = $_POST['subjectId'];

    // Iterate through each student's marks data
    foreach ($marksData as $studentId => $marks) {
        // Sanitize and validate input (optional, but highly recommended)
        $studentId = $conn->real_escape_string($studentId);
        $marks = $conn->real_escape_string($marks);

        // Update marks for the current student in the database
        $updateQuery = "UPDATE int_marks 
                        SET internal_marks = '$marks' 
                        WHERE student_id = '$studentId' 
                        AND exam_type = '$examId' 
                        AND class_id = '$classId' 
                        AND subject_id = '$subjectId'";
        $result = $conn->query($updateQuery);

        // Check if the update was successful
        if ($result) {
            // Marks updated successfully
            // You can handle success actions here if needed
        } else {
            // Error updating marks
            // You can handle error actions here if needed
            echo "Error updating marks for student ID: $studentId";
        }
    }

    // Close the database connection
    $conn->close();
} else {
    // Missing required parameters
    echo "Missing required parameters (marksData, examId, classId, or subjectId).";
}
?>
