数据库表单生成

I have a “t_form” table and a “t_attribute” table. It looks a little bit like below.

我有一个“ t_form”表和一个“ t_attribute”表。 看起来有点像下面。

Form table

表格表

form_id | form_name    | description
-----------------------------------
1       | Laptop  	   | Possible attributes are Model, Screen Size, OS, Cd Player
2       |Mobile  	   | Possible attributes are Model, OS
3       | Tablet  	   | Possible attributes are Model, Screen size, OS

Attribute table

属性表

attribute_id | attribute
------------------------
1  		     | Model
2       	 | Screen Size
3       	 | OS
4       	 | Cd Player
  1. Possible attributes of type Laptop form are Model, Screen Size, OS, Cd Player.

    笔记本电脑形式的可能属性是型号,屏幕尺寸,操作系统,Cd播放器。
  2. Possible attributes of type Mobile form are Model, OS.

    移动表单类型的可能属性是模型,操作系统。
  3. Possible attributes of type Tablet form are Model, Screen size, OS.

    平板电脑形式的可能属性是型号,屏幕尺寸,操作系统。

Form_Attribute table

Form_Attribute表

form_id | attribute_id
----------------------
1       | 1 
1       | 2
1       | 3
1       | 4
2       | 2
2       | 2
3       | 2
3       | 2
3       | 2

I will generate the HTML form like below(with form fields) according to the selection of form type(either Laptop, Mobile or Tablet) by the user from the database.

我将根据用户从数据库中选择的表单类型(笔记本电脑,移动设备或平板电脑)生成如下所示HTML表单(带有表单字段)。

For Laptop:

对于笔记本电脑:

enter image description here

For Mobile:

对于手机:

enter image description here

For Tablet:

对于平板电脑:

enter image description here

Questions:
Here are my questions.

问题:
这是我的问题。

  1. What is better approach to store the data entered by the user into the database.

    有什么更好的方法可以将用户输入的数据存储到数据库中。
  2. for example: I want to fetch the result which laptop screen size is avilable from 14 to 16.例如 :我想获取笔记本屏幕大小从14到16不等的结果。

Filter:

过滤:

enter image description here

Please provide a feasible solution with some example for better understanding to me and help me to achieve this.

请提供一个可行的解决方案,并提供一些示例,以使我更好地理解并帮助我实现这一目标。

What about a single table with the following fields:

包含以下字段的单个表呢?

type | Model | Screen Size | OS | Cd Player

type specified whether it is a mobile, laptop or tablet.

指定的类型是手机,笔记本电脑还是平板电脑。

Some fields (e.g. Cd Player) for some items (e.g. Mobile) are not used. But the processing/query on this single table is unified and is much easier.

某些项目(例如移动)的某些字段(例如Cd播放器)未使用。 但是,在此单个表上的处理/查询是统一的,并且容易得多。

This, of course, has the cost of some additional storage space in the DB.

当然,这需要在数据库中增加一些存储空间

This may not be the best solution. it should be feasible as far as I can tell.

这可能不是最佳解决方案。 据我所知,这应该是可行的。

Answered by anonymous.
匿名回答。


Thanks for your reply, but this is a sample list of category, but in real time I have a bunch of categories say 40 to 45, each category .i.e each form hold many fields say 15 to 18. Your approach consume very much of storage in the database. Yes, I agree you this approach query processing would be better but I want a better approach interms of less storage and optimized table design. Thanks.

感谢您的答复,但这是类别的示例列表,但是实时地我有很多类别,例如40到45,每个类别.ie每个表单都包含许多字段,例如15到18。您的方法会占用大量存储空间在数据库中。 是的,我同意您使用这种方法进行查询处理会更好,但是我希望在减少存储空间和优化表设计方面取得更好的效果。 谢谢。



I see.

我懂了。

Then, you original design looks good for this purpose. The data may be stored in one or several tables like:

这样,您的原始设计就可以很好地实现此目的。 数据可以存储在一个或几个表中,例如:

item_id | attribute_id | attribute

If the attribute has several types, you may need several tables for different attribute types (to save space). If there is only several attribute types, you may consider a similar solution as in this answer.

如果属性具有多种类型,则可能需要针对不同属性类型使用多个表(以节省空间)。 如果只有几种属性类型,则可以考虑使用类似于此答案的解决方案。

For querying, you need to join several tables.

为了进行查询,您需要连接多个表。



Can you please tell me with some sample data, I am not getting clear.
Tell me for entry for the form type laptop, I want to store the data (Possible attributes of type Laptop form are Model, Screen Size, OS, Cd Player) in the database. How to store this in the table as per the second approach?

您能告诉我一些示例数据吗,我不清楚。
告诉我输入笔记本电脑表单的内容,我想在数据库中存储数据(笔记本电脑表单类型的可能属性是Model,Screen Size,OS,Cd Player)。 如何按照第二种方法将其存储在表中?



Okay, one example:

好的,举个例子:

items table:

item_id | form_id (maybe type_id, which is better)
------------------------------------------------------------
9       |  1 (for laptop as in your question)

attributes table:

item_id | attribute_id | attribute
----------------------------------------------
9       |  1           |  "HP model"
9       |  2           |  "14"
9       |  3           |  "Linux"
9       |  3           |  "Golden CD Player"

You may use another attribute table to store 9 | 2 | "14" where 14 is an integer.

您可以使用另一个属性表来存储9 | 2 | "14" 9 | 2 | "14" 9 | 2 | "14" ,其中14是整数。

翻译自: https://www.systutorials.com/html-form-generation-from-the-database-and-store-value-into-the-database/

数据库表单生成

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐