2014-03-12 32 views
0

大家好,感谢您查看此问题。SQL数据库的方法 - 我需要你的建议

既然有人问什么,我做这行,这里是答案: 一名艺术家问我给他弄一个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 

任何帮助将不胜感激

+0

这是什么?你的家庭作业? – lascort

+0

一位艺术家让我让他成为一个网络应用程序来存储他所有的新音乐会等等。现在,当谈到添加乐器,艺术家等时,我可以有10个乐器,或者100个。一切都被设置成一个表格..有些数据是像位置,时间等固定的,但是这个其他字段是使用DOM动态添加的。 –

回答

2

我会去一个领域的做法。您可以有三列,分别为Name,Surnamefield_values。在field_values列中,存储一个表示数组的字符串,该数组代表什么可能是您的列。例如,运行:

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

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