在MySQL数据库中存储复选框值的最佳方法是什么? [英] What's the best way to store Checkbox Values in MySQL Database?

查看:502
本文介绍了在MySQL数据库中存储复选框值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,以该页面为例:单击此处

如果您查看便利设施"部分,则有3套复选框(房租中包含的单位功能",社区功能"和实用程序").

我的问题是:我如何使用PHP制作3个数组变量(例如$ unit_features,$ community_features和$ utilities_included)以存储哪些框未/未选中到三个字段中桌子?

更重要的是:如何以相同的数组格式将数据从表中拉出,以便可以查看/编辑/删除细节?

  • 注意,我已经尝试过无数次通过在表中使用单独的字段(如tinyints)来做到这一点的方法-但它变得笨重而且根本不优雅……我什至想到了制作一个对象上课,但又失败了.

解决方案

属性和便利设施之间存在多对多的关系.要对此建模,您需要一个单独的表,而不是可变的列数.

有一个表可以存储您的属性.

INSERT INTO property (id, address, square_footage...) VALUES (111, '123 Main St', 1234...)

有一张桌子可以存放所有可能的设施.

INSERT INTO amenities (id, type, description) VALUES (222, 'Unit Features', 'Air Conditioning');

对于某项物业所具有的每种便利,请在与这两项相关的表中插入一行:

INSERT INTO property_amenitities (property_id, amenity_id) VALUES (111, 222);

当您想知道特定属性具有哪些便利设施时,只需SELECT该表中该属性键的所有行.如果要打印所有便利设施的复选框,请从amenities表中的SELECT并对property_amenities表进行LEFT OUTER JOIN. property_amenities表中具有空值的那些行是未选中的框.

相关阅读.在本地BORDERS破产之前,您应该先从中获取有关关系数据库的书:)

First, take a look at this page as an example: Click here

If you view the 'Amenities' section, there are 3 sets of checkboxes (Unit Features, Community Features and Utilities Included in Rent).

My Question Is: How can I use PHP to make 3 array variables (e.g. $unit_features, $community_features and $utilities_included) to store which boxes are/are not checked into 3 respective fields in a table?

More importantly: How do I pull the data out of the table in the same array format so that specifics can be viewed/edited/deleted?

  • Note, I've tried countless times to do this by having separate fields in the table (as tinyints) - but it gets bulky and isn't elegant at all...I even thought of making an object class but failed yet again..

解决方案

You have a many-to-many relationship between properties and amenities. To model this, you need a separate table, not a variable number of columns.

There is one table which stores your properties.

INSERT INTO property (id, address, square_footage...) VALUES (111, '123 Main St', 1234...)

There is one table which stores all possible amenities.

INSERT INTO amenities (id, type, description) VALUES (222, 'Unit Features', 'Air Conditioning');

For each amenity a property has, insert one row into the table relating these two:

INSERT INTO property_amenitities (property_id, amenity_id) VALUES (111, 222);

When you want to know what amenities a specific property has, just SELECT all rows from this table for that property's key. When you want to print out checkboxes for all amenities, SELECT from the amenities table and do a LEFT OUTER JOIN to the property_amenities table. Those rows with null values from the property_amenities table are the boxes which are unchecked.

Related reading. You should pick up a book on relational databases from your local BORDERS before they go out of business :)

这篇关于在MySQL数据库中存储复选框值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆