zibo-dashboard/public/userarea/load_table_columns.php
2025-02-26 08:57:46 +01:00

36 lines
915 B
PHP

<?php
header('Content-Type: application/json');
require_once 'class/db-functions.php';
$response = ["success" => false, "columns" => []];
try {
if (!isset($_GET['table']) || empty($_GET['table'])) {
throw new Exception("Invalid table name.");
}
$table = preg_replace('/[^a-zA-Z0-9_]/', '', $_GET['table']); // Sanitize table name
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Retrieve table columns
$stmt = $pdo->query("SHOW COLUMNS FROM `$table`");
$columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (!$columns) {
throw new Exception("No columns found in table.");
}
$response["success"] = true;
$response["columns"] = $columns;
} catch (Exception $e) {
$response["message"] = $e->getMessage();
}
// Log per debugging
error_log(json_encode($response));
// Restituisce la risposta JSON
echo json_encode($response);