What's the best way of putting form data into a database when there's an unknown anmount of data? What's the best way of putting form data into a database when there's an unknown anmount of data? database database

What's the best way of putting form data into a database when there's an unknown anmount of data?


EAV is an anti-pattern in this case. You will end up with very convoluted logic just to retrieve a single set of data.

Your first approach is more maintainable and understandable to others.

When it comes to a boolean value, such as a checkbox value, I would use a bit/boolean field in the database, where a check mark would be a true and the fact that you didn't get it posted back would become a false.

The same thing stands for the EAV schema - keep them all in the DB, just mark the value as true or false, depending on what was posted.


You should parse your input first, and determine proper values for each item. Then you take the data and put in the database.

The code should not rely on the existance of fields in the form to determine what fields to put in the database. A form can easily be manipulated, so it could be used to change any field in the table, not just the ones corresponding to the fields that you put in the form.

The EAV schema is good if you have plenty of similar fields, or a dynamic set of fields. Perhaps you should store the text data in the regular way and the values from the checkboxes in a separate table.


Perhaps a little off topic, but to work around the checkbox functionality, add a hidden input with the same name before the checkbox and value - zero (or a value that means 'not set'):

<input type="hidden" name="blah" value="0"><input type="checkbox" name="blah" value="1">

in that way, if the checkbox isn't checked, you'll still get a value of 0 in your post - but if it is checked, you will get 1, because the last field of the same name is POST'ed.