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

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Retrieve the API key from the request headers
$headers = apache_request_headers();
$apiKey = isset($headers['API-Key']) ? $headers['API-Key'] : '';

// Check the API key
$validApiKey = "123456789005";
if ($apiKey !== $validApiKey) {
    echo json_encode(array('error' => 'Invalid API Key.'));
    mysqli_close($conn);
    exit();
}

// Retrieve the table name from the POST request
$tableName = isset($_POST['tableName']) ? mysqli_real_escape_string($conn, $_POST['tableName']) : '';

if (!empty($tableName)) {
    // Prepare the SQL query to select data from the specified table
    $sql = "SELECT Class_link, Type, id FROM `$tableName`"; // Use backticks for table name to handle reserved words or special characters
    $result = mysqli_query($conn, $sql);

    // Check if the query was successful
    if ($result) {
        $data = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
        }

        // Return data in JSON format
        echo json_encode($data);
    } else {
        echo json_encode(array('error' => 'Query failed: ' . mysqli_error($conn)));
    }
} else {
    echo json_encode(array('error' => 'Table name is missing.'));
}

// Close connection
mysqli_close($conn);
?>
