简体   繁体   中英

Inserting dynamic form data to database. PHP

I have problem, I am creating quite complex form. Some parts of form are created dynamically. Lets say if you select certain option from a drop-down, extra fields gets injected to the form.

What approach would be best to store that data? I would like to try and get-away without using multiple tables. Because I makes the whole application so much more complex.

I was thinking of initializing all possible values as "0" in my model. And then overwrite them with post data, and just store the whole array in the table. Anyone see any problems with this approach?

The necessity of using multiple tables in your model doesn't depend on how much data (how many fields) you have to store - it depends on the logic of your model. So if there is a logical reason to use relationships in your model (fe 1:n, n:m) JUST DO IT!!!

If you will not follow the basic rules in creating your model and will try fe to store all the data in one table, although it should be divided into many tables, you will very soon regret it. Any change in your code in the future will cost you much more work and at some point you will not understand your own code and will have to write it again, this time following the rules;)

And don't worry if the devoloping the right model costs a lot of work (lately I invested over two weeks in developing my model) - it really makes sense, because afterwards you can work much faster and more effectively with a well developed and planned model.

On the other hand there are situations, when storing over 100 and more fields in one table makes sense - it depends on the logic. So if you will provide some example, maybe one can say if you should work with one or more tables.

A lot depends on what you want to do with the form data later, and how often.

Serialized Single Field

In the simplest use cases you could base64_encode(serialize($data)) all the data and put that into a single column in the database.

  • Simple
  • Fast to insert
  • Easy to add/change input fields
  • Difficult AND Slow to search for values (particularly at scale)
  • Difficult to programmatically update should you need to make systematic changes to the data
  • Perfect if you always pull all of the data out of the db and never narrow your sql queries by data in the serialized string.

Metadata Table

Adding a second metadata.table could offer a little more flexibility. The 2nd table would have a foreign key reference to the main form submissions, a metadata name, and the value. This allows a very flexible many to one relationship that you can easily store, search, and manipulate. You can see examples of this in wordpress.

  • 2 tables, but still simple
  • Easy to add/change input fields
  • Much better searching via sql
  • Much easier to systematically update
  • Perfect if you don't always get all the data or have to narrow searches by the form data

And a different direction - You may also consider looking at Document based databases like MongoDB or CouchDB if you find yourself dealing with a lot of this type of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM