Reputation: 3765
I'm building a site and need to allow users to be able to create custom forms. The part I'm having trouble with is how to store the data from these forms. Since they are user created fields, there will be no corresponding field in the database. What would be the best way of going about this? I assume modifying the table is the incorrect way of doing so. Would putting all the data into an array, serializing it, and then storing it in a field be a good way?
Upvotes: 0
Views: 261
Reputation: 51421
Would putting all the data into an array, serializing it, and then storing it in a field be a good way?
While that would store the data, it would not make the individual elements queryable. It's an astoundingly bad long-term idea.
Take a look at the entity-attribute-value model. You should be able to represent the fields in a form and even the data filled in to the form using EAV backed up by some rigid foreign key checks.
Now, EAV isn't perfect. It can be tough to create certain queries against it because of MySQL's limitations, but that's nothing a bit of external code can't fix. It'll still be ten times better than serialized data.
Upvotes: 1
Reputation: 39323
I would store a table userforms {id, user, index, fieldname, fieldtype} and then userformresponses {id, userformid, fieldname, response}
Upvotes: 0
Reputation: 14365
Maybe you can provide a solid Form builder framework (or modify an existing solution) where every user created field controlled by you like this:
//input elements named by a custom namespace:
`<input type="text" name="mycustomelement[user_defined_name]" />`
and there you go: when user submits the form, you can catch all 'mycustomelement' element then you can serialize it to store in a db.
Upvotes: 0
Reputation: 199
maybe you can serialize the values of the complete form and storing it in a blob to the database.
Upvotes: 0