SQL数据库方法 - 我需要你的建议

时间:2014-03-12 18:07:48

标签: php sql database

大家好,感谢您查看此问题。

由于有人问我在做什么,这就是答案: 一位艺术家让我让他成为一个网络应用程序,以存储他所有的新音乐会等。现在,当谈到添加乐器,艺术家等,我可以有10个乐器,或者可能100个..一切都设置为表单..某些数据是固定的,如位置,时间等,但其他字段是使用DOM动态添加的。

我正在建立一个系统,用户在其中设置一个表格,存储在数据库中,如:

Name,Surname,field_1
//Lets say that this is the "fixed" part of the form
//But the user should be able to add 'n' other fields with no limit
//Therefore my problem is that i would end up with a row made of, lets say,
//4 colums
//And another one of, maybe, 100 columns
//
//Then i will need to access these rows, and row one should have 4 cols, row two 100..
//This can't be done in a "traditional" way since each row should have the
//same amount of cols
//
//I thought to create a new table for each submission 
//but this doesn't really make that much sense to me..
//
//Storing all the possible fields in a single one and then 
//access them through an array ? That would require too much, even since my fields
//should have the possibility to be edited..
//Each field is a mixture of variables then, like 
//field1:a=12,field2:b=18.. too complex

非常感谢任何帮助

2 个答案:

答案 0 :(得分:2)

我会选择一种方法。您可以有三列,NameSurnamefield_values。在field_values列中,存储一个数组的PHP serialized字符串,表示本来是您的列的内容。例如,运行:

array(
    ['col1'] => 'val',
    ['col2'] => 'val1',
    ['col3'] => 'val2',
    ['col4'] => 'val3'
)

通过serialize()会给你:

a:4:{s:4:"col1";s:3:"val";s:4:"col2";s:4:"val1";s:4:"col3";s:4:"val2";s:4:"col4";s:4:"val3";}

并且您可以获取此值并通过unserialize()运行它以恢复您的阵列并根据需要使用它。加载/保存此数组中的数据并不比在序列化之前更改数组中的值然后将其保存到field_values列更困难。

使用这种方法,您可以拥有尽可能多的列数。如您所需,无需大量的柱子或桌子。

答案 1 :(得分:1)

在这种情况下,我会亲自为每个用户创建一个新表,并为新的自定义字段插入新行。您必须拥有一个包含每个用户表的表名的主表,以便以后访问该数据。

相关问题