<?php
$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);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $emis_code = $_POST['emis_code'][0]; // Assuming the same EMIS code for all posts

    // Prepare an SQL statement
    $stmt = $conn->prepare("INSERT INTO posts (emis_code, post_name, male, female, post_as_per_budget, filled_post, vacant_post) VALUES (?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssiiiii", $emis_code, $post_name, $male, $female, $post_as_per_budget, $filled_post, $vacant_post);

    // Loop through each set of post fields
    foreach ($_POST['post_name'] as $key => $value) {
        $post_name = $_POST['post_name'][$key];
        $male = $_POST['male'][$key];
        $female = $_POST['female'][$key];
        $post_as_per_budget = $_POST['post_as_per_budget'][$key];
        $filled_post = $_POST['filled_post'][$key];
        $vacant_post = $_POST['vacant_post'][$key];

        // Execute the prepared statement
        $stmt->execute();
    }

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

    // Redirect to a confirmation page (optional)
    header("Location: t2.php");
    exit();
}
?>
