49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION["user_id"])) {
|
|
header("Location: auth/login.php");
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
|
|
$nombre = trim($_POST['nombre']);
|
|
$orden = filter_input(INPUT_POST, 'orden', FILTER_VALIDATE_INT);
|
|
|
|
if ($id === 0 || empty($nombre) || $orden === false || $orden < 0) {
|
|
$_SESSION['error_message'] = "Datos inválidos para actualizar la ciudad.";
|
|
header("Location: ciudades.php");
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$sql = "UPDATE ciudades SET nombre = :nombre, orden = :orden WHERE id = :id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->bindParam(':nombre', $nombre, PDO::PARAM_STR);
|
|
$stmt->bindParam(':orden', $orden, PDO::PARAM_INT);
|
|
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
|
|
if ($stmt->execute()) {
|
|
$_SESSION['success_message'] = "Ciudad actualizada exitosamente.";
|
|
} else {
|
|
$_SESSION['error_message'] = "Error al actualizar la ciudad.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
if ($e->errorInfo[1] == 1062) {
|
|
$_SESSION['error_message'] = "Error: Ya existe una ciudad con ese nombre.";
|
|
} else {
|
|
$_SESSION['error_message'] = "Error de base de datos: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
header("Location: ciudades.php");
|
|
exit();
|
|
} else {
|
|
header("Location: ciudades.php");
|
|
exit();
|
|
}
|
|
?>
|