As many of you viewing this may know there is always a right way to do something and a wrong way to do something. So I've come here to see if I could maybe seek some guidance.

What I have done is create a dynamic set of form fields (I can add and remove rows of fields using two buttons), this is centered within a huge main form (this is only one small part). I am honestly thinking of submitting this stuff as an array, however throughout my hours of research on how to properly do this I see everywhere saying you shouldnt submit text inputs as arrays. This part of the form is laid out as follows:
Datetime | Name | Quantity | Phone Number | Status

The datetime field is currently a text input, I will be upgrading that soon to be a calender picker to ensure that data is submitted to the mySQL database in the appropriate format and to make it a bit more user friendly. The name, quantity, and phone number fields are also text inputs, while the status field is actually a drop-down select input.

Now there are other form elements surrounding this "centralized" area of the form. Currently submitting the form sends the majority of the data I have to one mysql table in the database (including a unique identifier). The data in the part of the form described above will actually be going to a second data table (along with the same unique identifer). This way when I search the unique identifer in theory the form data will be filled in using data from both table which is tied together with that unique identifer. The idea behind this is that the main part of the form will be one single entry in the main mySQL table, while this dynamic form data might result in 1-30+ mySQL data rows within its own table.

So now that you hopefully have an idea of the setup of the form, my question is this....if I name each form input kinda like this:

            <td><input name="datetime[]" type="text"></td>
            <td><input name="name[]" type="text"></td>
            <td><input name="quantity[]" type="text"></td>
                <select name="status[]">
                    <option value="" selected="selected">--</option>

And make an array out of the data, will this work? Is this (submitting it as an array) the best way to do it? I have been fooling around with it for three days now and have been encountering alot of problems. I am tempted just out of frustration to scrap the dynamic form fields and just make a maximum of 20 fields which are display:none'd via CSS and giving each individual form input its own name.

Member Avatar

And make an array out of the data, will this work? Is this (submitting it as an array) the best way to do it? I have been fooling around with it for three days now and have been encountering alot of problems.

Since you post this at php section.

Post your php code that has the array and the query that is related to the form?

It's bit hard for anyone to understand what you are doing.

You only post the html code but what you just mention you didn't post any php/mysql code?

This isnt a question on whether code is right or wrong, so posting the code wont make a difference. My question is when you have multiple text inputs that are dynamically generated, what is the best way to get the inputted data to mysql? Forcing the inputs to put the data into an array and passing it that way?

Again my question isnt being posted to ask for code or look for solutions, I'll figure that out on my own. What I am looking for is merely to be given guidance on the best approach to my dynamic form field issues using PHP.

I have attached the above photo to give you an example of what I am doing. Doing the "Add Row" button adds another field of data. So thats why I was originally thinking of doing it as an array which seemed at the time to be a great solution because of the fact that the number of fields could at any time change (being that they are dynamically generated). However everywhere I look, I keep hearing the advice not to try and insert with php with data from text inputs as an array to MySQL with little or no explanation as to why.

Passing as an array is exactly what you should be doing. Not sure whee you have heard that passing form data as an array is wrong as that is the only way to pass the data if you have multiple fields of the same 'type'.

Member Avatar

You can add multiple rows from the data with the VALUES syntax...

INSERT INTO table (fields...) VALUES (row1 values),(row2 values),(row3 values)...

For an old mysql_query, you need to use mysql_real_escape_string() on all inputs, so you can either do it individually in a loop to build the statement or do it before as an array_map().

If you're using mysqli or PDO, then you needn't worry about escaping and you can build your array to bind from the $_POST vars. However, it takes a bit of doing.

Hey guys thanks for the responses. I'm glad to know I'm on track and not wasting my time. @diafol, ya I'm doing it via mySQLi.