views:

135

answers:

2

Hello. I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.

I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.

Thank you!

A: 

You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.

If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.

Note: this method only working when you don't need to search for specific items in your database.

Edit: maybe i'm misunderstood your problem.

You can create a 'metadata' table like this:

form_id  |  option_name  | option_value
---------------------------------------
1        |  hotel        | true
1        |  restaurant   | false
fabrik
Yeah, that's the thing. I have to access each field at a specified time from database. The fields are different, different names. The problem is that there could be 30 fields submitted or 15, based on user selection.
Manny Calavera
It's no problem. This type of database design you can store any number of options per form and even you can extend it with more columns like user_id of submitter, timestamp of save, etc. Even more, if you don't need to store these datas you can prune 'uninteresting' rows based on a specific timestamp.
fabrik
My concern is how to design the database so I can store 15 fields and also 30 fields. At a time, the user can select in form so that it ends up with 30 fields and it needs to submit them to database. And next time a user can choose so that it ends up with 15 or 20 fields to be stored in database. So the database should be capable to store all possible or just 5 or 10. I hope you understand my request.
Manny Calavera
I think my solution is easily capable for that.
fabrik
A: 

So it's very dynamic here and I don't know which would be the best approach for storing every form field in database.

I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:

If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.

Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".

When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.

If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.

If you can post in a specific example of something you don't know how to do, that would be useful.

Rupert
Well, this is the exact situation that you posted. And I believe it would work this way. I would create multiple tables to store every piece of form. I will try to split the form in different parts and hook them up whenever I need to display the data. Thanks and sorry that my description was vague.
Manny Calavera