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


SQL Database approach - I need your suggestion

大家好,感谢大家观看这个问题。

既然有人问我这样做是为了什么,下面是答案:一个艺术家让我为他做一个web应用程序来存储他所有的新演唱会等等。现在,当涉及到添加乐器,艺术家等时,我可以有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

任何帮助都将非常感谢

我会选择一个字段的方法。你可以有三列,Name Surnamefield_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列更困难。

使用此方法,您可以根据需要拥有尽可能多或少的"列",而不需要大量的列或表。

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