<?php
$servername = "localhost";
$username = "venmobdc_Auto_DataBase_User";
$password = "hB_nf_W-^SIh";
$dbname = "venmobdc_Auto_Database";

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

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Sanitize and validate the table name from POST request
    $tableName = isset($_POST['tableName']) ? $conn->real_escape_string(trim($_POST['tableName'])) : '';

    if (!empty($tableName)) {
        // Prepare the SQL query to retrieve both Year and Table_Name columns from the specified table
        $sql = "SELECT Year, Table_Name FROM `$tableName`";

        // Execute the query
        $result = $conn->query($sql);

        if ($result) {
            if ($result->num_rows > 0) {
                $data = array();

                // Fetch each row of data
                while($row = $result->fetch_assoc()) {
                    $data[] = $row;
                }

                // Return data as JSON
                echo json_encode($data);
            } else {
                echo json_encode(array("message" => "No records found."));
            }
        } else {
            echo json_encode(array("message" => "Query error: " . $conn->error));
        }
    } else {
        echo json_encode(array("message" => "Invalid table name."));
    }
} else {
    echo json_encode(array("message" => "Invalid request method."));
}

$conn->close();
?>
