简体   繁体   English

在 MySQL 数据库中存储复选框值的最佳方法是什么?

[英]What's the best way to store Checkbox Values in MySQL Database?

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).如果您查看“设施”部分,则有 3 组复选框(单元功能、社区功能和租金中包含的实用程序)。

My Question Is: How can I use PHP to make 3 array variables (eg $unit_features, $community_features and $utilities_included) to store which boxes are/are not checked into 3 respective fields in a table?我的问题是:如何使用 PHP 制作 3 个数组变量(例如 $unit_features、$community_features 和 $utilities_included)来存储哪些框是/未检入表中的 3 个相应字段?

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..请注意,我已经无数次尝试通过在表中使用单独的字段(作为 tinyints)来做到这一点 - 但它变得笨重而且一点也不优雅......我什至想过制作 object class 但又失败了。 .

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.到 model 这个,你需要一个单独的表,而不是可变数量的列。

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.如果您想知道特定属性具有哪些便利设施,只需SELECT此表中的所有行即可获取该属性的键。 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.当您想打印所有便利设施的复选框时, SELECTamenities表中,并对property_amenities表执行LEFT OUTER JOIN Those rows with null values from the property_amenities table are the boxes which are unchecked. property_amenities表中具有 null 值的那些行是未选中的框。

Related reading .相关阅读 You should pick up a book on relational databases from your local BORDERS before they go out of business:)你应该从当地的 BORDERS 那里拿起一本关于关系数据库的书,然后他们 go 倒闭了:)

If they are not mutually exclusive;如果它们不是相互排斥的; you can use bitwise operators to store them.您可以使用按位运算符来存储它们。

field = 0;
if($unity_features_selected == true) {
  field |= (1<<0);
}
if($community_features_selected == true) {
  field |= (1<<1);
}
if($utilties_included_selected == true) {
  field |= (1<<2);
}

then store field as an unsigned int.然后将字段存储为无符号整数。 This lets you query for (ex. unity_features) such as这使您可以查询(例如 unity_features),例如

select * from <table> where field&(1<<0);

Or community features:或社区功能:

select * from <table> where field&(1<<1);

1) php serialize and unserialize ( http://php.net/manual/en/function.serialize.php ) will help you keep arrays or objects or any other structural data in db. 1) php serialize and unserialize ( http://php.net/manual/en/function.serialize.php ) will help you keep arrays or objects or any other structural data in db.

2)use array format for input's names like described here: Submitting a multidimensional array via POST with php 2)使用数组格式输入名称,如下所述: Submitting a multidimensional array via POST with php

3)remember that checkboxes unchecked won't be send at all, so always make one hidden input field for every checkbox field. 3)请记住,未选中的复选框根本不会发送,因此请始终为每个复选框字段创建一个隐藏的输入字段。 That will ensure, that unchecked checbox will be sent as false.这将确保未选中的复选框将作为假发送。 Here is an example: Post the checkboxes that are unchecked这是一个示例: 发布未选中的复选框

So reasuming - you could sent that way thre arrays, with falues for all checkboxes (1 or 0), and store them in three fields as serialized objects.如此令人欣慰 - 您可以通过 arrays 的方式发送所有复选框(1 或 0),并将它们作为序列化对象存储在三个字段中。

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

相关问题 在数据库中存储多个复选框值并在以后使用它的最佳方法是什么 - What is the best way to store multiple checkbox values in the database and work with it later on 将复选框值存储到数据库的最佳方法? - Best way to store checkbox values to database? 将html数据存储在mysql数据库中的最佳方法是什么? - What's the Best way to store html data in a mysql database? 在 MySQL 数据库中存储日期和时间以供以后使用 PHP 显示的最佳方法是什么? - What's the best way to store date and time in a MySQL database for later display with PHP? 在MySQL数据库中存储位掩码值数组的最佳方法? - Best way to store an array of bitmasked values in a MySQL database? 在MySQL数据库中存储特殊字符的最佳方法是什么? - What is the best way to store special characters in a MySQL database? 在一个mysql列中存储键值列表的最佳方法是什么 - What is the best way to store a list of key values in one mysql column 在Mysql上存储大量表单信息的最佳方法是什么? - What's the best way to store a lot of form information on a Mysql? 在 MySQL 中构建和存储部分数据的最佳方法是什么? - What's the best way to structure and store part of data in MySQL? 在MySQL中存储敏感数据的最佳方法是什么? - What's the best way to store sensitive data in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM