prepare('SELECT id, name FROM locations WHERE id = ?'); $stmt->execute([$location_id]); $location = $stmt->fetch(); if (!$location) { header('Location: locations.php'); exit; } } catch (PDOException $e) { $error_message = 'Database error: ' . $e->getMessage(); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; if (empty($name)) { $error_message = 'Please fill in the location name.'; } else { try { $pdo = db(); $stmt = $pdo->prepare('SELECT id FROM locations WHERE name = ? AND id != ?'); $stmt->execute([$name, $location_id]); if ($stmt->fetch()) { $error_message = 'A location with this name already exists.'; } else { $sql = "UPDATE locations SET name = ? WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$name, $location_id]); header("Location: locations.php?success=location_updated"); exit; } } catch (PDOException $e) { $error_message = 'Database error: ' . $e->getMessage(); } } } ?>