T
T
TopClans2015-01-02 10:38:34
PHP
TopClans, 2015-01-02 10:38:34

How to force PHP to write multiple data at once from one form to one mysql table?

Hello everyone, I'm new to PHP, I decided to make a small site for ordering for practice.
There is an order table, which contains the order number and customer data.
And the order_items table, where the order number contains the data of the ordered goods: name, article, price, quantity.
UPD: Each order will have its own products, and they will not be reused later.
Also now I'm making a table with a form for adding an order. Due to the fact that the number of ordered products is always different, I had a blunt: how to actually make PHP accept a whole bunch of lines (for example, we order 5 products - then we have 5 sets of fields with the name, article, price, quantity) and then each add to table?
The most understandable but inconvenient method is to make one form on the page with the form for adding an order and add the goods one by one: first the first, then reload the table and add the second, third. But I want to be able to enter all the data at once.
A less understandable method is to somehow drive all the data from the form into an array, and then use WHILE (there are rows in the array) to add them to the database.
The third method is to spawn, for example, 20 inputs with names: name1, sku1, price1, quantity1, name2, sku2, price2, quanity2, etc., and then spawn 20 IFs (in the field name1/name2/etc) there is something , then add the data from sku1/sku2 and so on to the database, respectively.
But I feel there is some simple and elegant solution. Can someone tell me?
UPD:for ease of understanding my problem, imagine that it is about registering a user / adding any other record in MySQL.
So, I need to add several users at once. It is necessary to display, for example, 10 sets of fields (login password mail), then those fields that are filled in - add to the database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
seriogja, 2015-01-02
@seriogja

And the order_items table, where the order number contains the data of the ordered goods: name, article, price, quantity.
This is the wrong approach. The order_items table needs to store information about the order (time, customer id, etc.). For a many-to-many relationship, you need to use a join table that contains the id of the order and the ids of the items in that order.

E
Egor Stakhovsky, 2015-01-02
@ySky

  1. Create form elements using Javascript (leave the quantity at the mercy of the user, let fields be created for each product, say, by a button), giving the fields a certain prefix and an incremental index (art1, price1, quantity1, etc.). Pass this index to the processing script in $_post (and the values ​​of all form fields, of course).
  2. Make functions that will take variables from $_post into an array using a loop ($_post["art".i], where i is from the start of the incremental index to its value at the time of sending).
  3. Create a function that will iterate through the resulting arrays and add the data to the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question