<?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("Connection failed: " . $conn->connect_error);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $tableName = isset($_POST['tableName']) ? $conn->real_escape_string(trim($_POST['tableName'])) : '';

    if (!empty($tableName)) {
        // Adjusted SQL query to select both `Department` and `Department2` columns
        $sql = "SELECT Department, Department2 FROM `$tableName`";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            $data = array();
            while($row = $result->fetch_assoc()) {
                $data[] = $row;
            }
            echo json_encode($data);
        } else {
            echo json_encode(array("message" => "No records found."));
        }
    } else {
        echo json_encode(array("message" => "Invalid table name."));
    }
} else {
    echo json_encode(array("message" => "Invalid request method."));
}

$conn->close();
?>
