<?php
header('Content-Type: application/json');

// Database connection parameters
$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(["error" => "Connection failed: " . $conn->connect_error]));
}

// Check if the selected table name is provided in the request
if (isset($_POST['selectedTable'])) {
    $selectedTable = $conn->real_escape_string($_POST['selectedTable']);
    
    // Query the specified table for data
    $sql = "SELECT * FROM `$selectedTable`";
    $result = $conn->query($sql);
    
    if ($result === false) {
        die(json_encode(["error" => "SQL error: " . $conn->error]));
    }

    $data = array();
    
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
    }
    
    // Return the data as JSON
    echo json_encode($data);
} else {
    echo json_encode(["error" => "No table name provided."]);
}

$conn->close();
?>
