Reputation: 10791
I need some advice before I start. My PHP Skills are only 3 months old so learning as I go.
I need to create an order capture screen that captures the information to a database.
The order form can have up to 150 lines that all need to be captured.
I can create the form easy enough but am wondering what the best way to write this to the database is.
on submitting, I can get the form values using the post method and write them to mysql using:
INSERT INTO example
(example_id, name, value, other_value)
VALUES
(100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2'),
(102, 'Name 3', 'Value 3', 'Other 3'),
(103, 'Name 4', 'Value 4', 'Other 4');
Is there a more streamline method of doing this or do I need to repeat the code for 150 lines?
Upvotes: 1
Views: 171
Reputation: 28906
From a usablility standpoint, you may wish to reconsider the design of your form. If the user fills out 149 lines of the order form and accidentally hits the back button, they are going to have a really bad day. Perhaps you should consider capturing a single line at a time, or allowing the user to upload a file (spreadsheet, CSV, etc) containing all of the entries.
If you cannot change the design of the form, you will need to process the lines in a loop. The loop can look something like this:
for ( $i = 1; $i <=150; $i++ ) {
$name = $_POST['name' . $i];
$value = $_POST['value1' . $i];
// capture the rest of the field values
$query = "INSERT INTO (...) VALUES ($name, $value, ...)";
mysql_query($query);
}
Alternately, you can append the result of each loop to one big query, and run it after capturing all 150 rows.
For the sake of brevity, I have left out a few details which you need to add:
Upvotes: 1