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

if (isset($_POST['student_id']) && isset($_POST['changedSubjects'])) {
    $studentId = $_POST['student_id'];
    $changedSubjects = $_POST['changedSubjects'];

    // Prepare the update query for each changed subject
    foreach ($changedSubjects as $subjectField => $newCode) {
        $updateQuery = "UPDATE studentsub SET $subjectField = ? WHERE student_id = ?";
        $stmtUpdate = $conn->prepare($updateQuery);
        $stmtUpdate->bind_param("si", $newCode, $studentId);
        $stmtUpdate->execute();
        $stmtUpdate->close();
    }

    echo json_encode(["status" => "success", "message" => "Subjects updated successfully."]);
} else {
    echo json_encode(["status" => "error", "message" => "Invalid request."]);
}

$conn->close();
?>
