<?php
// Database configuration
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "esekotli";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve EMIS Code
$emisCode = $_POST['emisCode'];

// Prepare SQL to insert data
$stmt = $conn->prepare("INSERT INTO school_students (emis_code, class_name, total_male, total_female, repeating_male, repeating_female, orphan_male, orphan_female, special_male, special_female, scholar_male, scholar_female) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

// Bind parameters and execute
foreach ($_POST['Class_name'] as $index => $className) {
    $totalMale = $_POST['totalMale'][$index] ?? 0;
    $totalFemale = $_POST['totalFemale'][$index] ?? 0;
    $repeatingMale = $_POST['repeatingMale'][$index] ?? 0;
    $repeatingFemale = $_POST['repeatingFemale'][$index] ?? 0;
    $orphanMale = $_POST['orphanMale'][$index] ?? 0;
    $orphanFemale = $_POST['orphanFemale'][$index] ?? 0;
    $specialMale = $_POST['specialMale'][$index] ?? 0;
    $specialFemale = $_POST['specialFemale'][$index] ?? 0;
    $scholarMale = $_POST['scholarMale'][$index] ?? 0;
    $scholarFemale = $_POST['scholarFemale'][$index] ?? 0;

    $stmt->bind_param("ssiiiiiiiiii", $emisCode, $className, $totalMale, $totalFemale, $repeatingMale, $repeatingFemale, $orphanMale, $orphanFemale, $specialMale, $specialFemale, $scholarMale, $scholarFemale);
    $stmt->execute();
}

// Close connection
$stmt->close();
$conn->close();

echo "Data saved successfully!";
?>
