什么更好?国家、州和城市是否有数据库?
我正在使用 PHP 和 PHP 开发一个网站。 MySQL用户可以在其中注册。在注册表中,他们需要填写国家、州和城市。我想知道将其保存在我的数据库中是否是个好主意。我很困惑,因为 Facebook、谷歌和其他公司都有自动完成表单,这些输入标签会建议您所在的国家/地区、州甚至城市。
I'm developing a website with PHP & MySQL where users can sign up. In the sign up form they need to fill up country, state and city. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是您是否想让它适用于世界上的每个人,或者仅适用于美国地址 - 以及您尝试收集信息的目的。
如果您的网站是电子商务网站并且您想要收集送货地址,则您需要对您的输入进行相当高质量的验证,否则 UPS 将不会将这些内容交付给您的客户。
另一方面,如果您只是想很好地了解人们在哪里,但邮政地址质量并不重要,只需使用像 PlaceFinder 这样的免费 Web 服务
http://developer.yahoo.com/geo/
它应该允许您的用户只需输入地址,而无需将其分成城市/state/country 并仅存储生成的地点 ID (woeid) 在您的数据库中。
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.
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
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.
一般来说,我建议您拥有只读的国家/地区列表,这样用户只能选择而不能添加。然后,对于州和引用,有一个预加载的列表,但允许用户添加一个。 (您可以从任何您想要的互联网数据库或服务(例如雅虎的数据库或服务)预加载此列表。
在所有情况下,建议对于他们(因为可用性)和您(因为它会减少数据重复)都是一个非常好的加分,
希望这会有所帮助。干杯
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).
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
还要考虑到,有些城市具有相同的名称,但位于不同的国家/地区。里加是拉脱维亚的首都,里加位于美国密歇根州。
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.
这取决于您的业务的要求。如果您想提供区域化服务(根据用户的地理区域进行日志记录),那么是的,您绝对应该在数据库中保存这些区域。
另一方面,您必须设计数据库来保存您的区域(至少有 3 个具有 1-n 关系的表或 1 个具有父/子关系的表)并用有效数据填充该数据库 - 您是否拥有所有国家/地区/来自世界各地的州和城市?
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.
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?
使用上述结构进行城市到国家的地图绘制。您还可以标记顶级城市和国家。
Use the above structure for city to country mapping. You can also mark top cities and countries.
您不应单独保存所有三个,例如
country_id
、state_id
和city_id
。原因是这是冗余信息。一座城市隐含地属于一个州,属于一个国家。通过将它们保存为单独的值,您不会获得任何信息,只会带来弄乱数据的风险(因为您可以将日本、华盛顿、柏林保存为一组,这是无效且不一致的信息)。您应该在数据库中创建一个层次结构树。城市有一个母州,有一个母国。然后你只需要询问用户他们的城市,所有其他信息都是隐式的。从哪里获取数据是另一个主题。例如,您可以尝试 Google 的 API。
You should not save all three separately, like
country_id
,state_id
andcity_id
. The reason being that this is redundant information. A city implicitly belongs to a state belongs to a country. 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. 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.