49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
require_once 'conexion.php';
|
|
header('Content-Type: application/json');
|
|
|
|
try {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!isset($data['columna'])) {
|
|
throw new Exception("Columna no especificada");
|
|
}
|
|
|
|
$columna = trim($data['columna']);
|
|
|
|
// Validar que sea un nombre válido
|
|
if (empty($columna)) {
|
|
throw new Exception("Columna inválida");
|
|
}
|
|
|
|
// Convertir nombre a formato de columna
|
|
$nombre_columna = strtolower(str_replace(' ', '_', $columna));
|
|
|
|
// Verificar que la columna existe
|
|
$sql_check = "SHOW COLUMNS FROM gastos LIKE '" . $conexion->real_escape_string($nombre_columna) . "'";
|
|
$result_check = $conexion->query($sql_check);
|
|
|
|
if ($result_check === false || $result_check->num_rows === 0) {
|
|
throw new Exception("La columna de gasto no existe:");
|
|
}
|
|
|
|
// Eliminar la columna
|
|
$sql_drop = "ALTER TABLE gastos DROP COLUMN `$nombre_columna`";
|
|
|
|
if (!$conexion->query($sql_drop)) {
|
|
throw new Exception("Error al eliminar columna: " . $conexion->error);
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Gasto eliminado correctamente'
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|