beginTransaction(); // Set location_id to NULL for assets associated with this location $stmt = $pdo->prepare('UPDATE assets SET location_id = NULL WHERE location_id = ?'); $stmt->execute([$location_id]); // Delete the location $stmt = $pdo->prepare('DELETE FROM locations WHERE id = ?'); $stmt->execute([$location_id]); $pdo->commit(); header("Location: locations.php?success=location_deleted"); exit; } catch (PDOException $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } // In a real app, log this error. header("Location: locations.php?error=db_error"); exit; }