简体   繁体   English

什么更好? 国家、州和城市是否在数据库中?

[英]What's better? Countries, States and Cities on DataBase or not?

I'm developing a website with PHP & MySQL where users can sign up.我正在开发一个带有 PHP 和 MySQL 的网站,用户可以在其中注册。 In the sign up form they need to fill up country, state and city.在注册表单中,他们需要填写国家、state 和城市。 I want to know if it's a good idea to save it on my database or not.我想知道将它保存在我的数据库中是否是个好主意。 I'm confused because Facebook, google, and others have autocomplete forms where those input tags suggest you country, state and even cities.我很困惑,因为 Facebook、google 和其他人有自动完成 forms ,其中这些输入标签建议您国家、state 甚至城市。

Question is whether you want to make it work for everybody in the world, or just for US addresses -- and also for what purpose you are trying to gather the information.问题是你是否想让它适用于世界上的每个人,或者只适用于美国地址——以及你试图收集信息的目的是什么。

If your site is ecommerce and you want to collect shipping addresses, you will need to have a fairly good quality of validation of your input -- otherwise UPS will not deliver the stuff to your customers.如果您的网站是电子商务网站并且您想收集送货地址,则您需要对您的输入进行相当高质量的验证——否则 UPS 不会将这些东西交付给您的客户。

If on the other hand, you just want to have a fairly good idea of where people are, but that it is not essential to have postal address quality, just use a free web service like PlaceFinder另一方面,如果您只是想对人们的位置有一个相当好的了解,但邮政地址质量并不重要,只需使用免费的 web 服务,如 PlaceFinder

http://developer.yahoo.com/geo/ http://developer.yahoo.com/geo/

It should allow you to let your users to just type in the address without splitting it up into city/state/country and just store the resulting place ID (woeid) in your database.它应该允许您让您的用户只输入地址而不将其拆分为城市/州/国家/地区,并且只需将生成的地点 ID (woeid) 存储在您的数据库中。

You should not save all three separately, like country_id , state_id and city_id .您不应该单独保存所有三个,例如country_idstate_idcity_id The reason being that this is redundant information.原因是这是冗余信息。 A city implicitly belongs to a state belongs to a country.一个城市隐含地属于一个 state 属于一个国家。 You are not gaining any information by saving them as separate values, and are only introducing the risk of messing up the data (since you could save Japan, Washington, Berlin as a set, which is invalid and inconsistent information).通过将它们保存为单独的值,您不会获得任何信息,只会引入混淆数据的风险(因为您可以将日本、华盛顿、柏林保存为一组,这是无效且不一致的信息)。

You should create an hierarchical tree in your database.您应该在数据库中创建一个层次树。 Cities have a parent state have a parent country.城市有父 state 有父国家。 Then you only need to ask the user for their city and all the other information is implicit.然后你只需要向用户询问他们的城市,所有其他信息都是隐含的。 Where you get that data from is a different topic.从哪里获取数据是一个不同的主题。 You can try Google's API, for instance.例如,您可以尝试 Google 的 API。

Generally, I'd recommend you to have read-only list of Countries, so user can only choose but not add.一般来说,我建议你有只读的国家列表,所以用户只能选择但不能添加。 Then, for states and cites, have a pre-loaded list of them, but allow the users to add one.然后,对于州和引用,有一个预先加载的列表,但允许用户添加一个。 (You can preload this lists from any internet database or service you want, like yahoo's). (您可以从任何您想要的 Internet 数据库或服务中预加载此列表,例如 yahoo 的)。

In all cases, suggesting is a very good plus, for them (because of usability) and for you (because it will reduce data duplication)在所有情况下,建议对他们(因为可用性)和您(因为它会减少数据重复)来说都是一个很好的加分项

Hope this helps.希望这可以帮助。 Cheers干杯

That depends on the requirements of your business.这取决于您的业务需求。 If you want to offer regionalized services (having logc based on the geographic region of users) then yes, you should definitely hold the regions in the DB.如果您想提供区域化服务(具有基于用户地理区域的 logc),那么是的,您绝对应该将这些区域保存在数据库中。

On the other side, you will have to design your DB to hold your regions (with at least 3 tables in 1-n relationship or 1 table with parent/child relationship) and fill that DB with valid data - do you have all the countries/states and cities from all over the world?另一方面,您必须设计您的数据库来保存您的区域(至少有 3 个表处于 1-n 关系或 1 个表具有父/子关系)并用有效数据填充该数据库 - 您是否拥有所有国家/地区/来自世界各地的州和城市?

CREATE TABLE `pl_countries` (
  `countryID` int(4) NOT NULL auto_increment,
  `iso` char(2) NOT NULL,
  `name` varchar(80) NOT NULL,
  `printable_name` varchar(80) NOT NULL,
  `iso3` char(3) default NULL,
  `numcode` smallint(6) default NULL,
  `top_flag` tinyint(1) NOT NULL,
  `status` tinyint(1) NOT NULL,
  PRIMARY KEY  (`countryID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE `cities` (
  `city_id` bigint(20) NOT NULL auto_increment,
  `countryID` bigint(20) NOT NULL,
  `city` varchar(30) NOT NULL default '',
  `top_city` tinyint(1) NOT NULL,
  PRIMARY KEY  (`city_id`),
  KEY `country_id` (`country_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Use the above structure for city to country mapping.使用上述结构进行城市到国家的映射。 You can also mark top cities and countries.您还可以标记顶级城市和国家。

Also take into account, that there are cities which have the same name but are located in separate countries.还要考虑到,有些城市名称相同但位于不同的国家。 Riga is a capital city of Latvia and also Riga is in USA, Michigan.里加是拉脱维亚的首都,里加位于美国密歇根州。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM