There is a table in which data is edited and a button for which this is all saved. The request initially looked like this:

$SQL = "UPDATE Users SET phone='$phone', name='$name', surname='$surname', second_name='$second_name', manager_comment='$manager_comment', delivery_adress='$delivery_adress', delivery_time='$delivery_time', delivery_date='$delivery_date', courier='$courier' WHERE id='$form_id'"; $result = mysqli_query($link, $SQL); 

But he writes only 1 line. How to rebuild a query so that it makes multiple entries for each id?

  • one
    Run a query in a loop - DaemonHK
  • What to do in any case impossible. - Maxim Stepanov
  • @ MaximStepanov can - Ipatiev
  • So you can wipe the ass with your finger as long as no one sees you. I will specify - if you are a programmer, you cannot. And if you do three thousand sites, everything is possible in principle. So you are, to some extent, right. - Maxim Stepanov
  • @MaximStepanov, do not confuse a qualified programmer with a dogmatist who heard a ring somewhere, but did not understand the meaning :) - Ipatyev

2 answers 2

As DaemonHK correctly pointed out, there is no problem running the query in a loop.

This will be a simple and understandable option, for its implementation it is not necessary to "figure out how to transfer data in the form of such a table." In programming, you should always avoid unnecessary over-complication of the code from scratch.

But for the prepared expressions, about which you are being told for the umpteenth time, several requests in a cycle are just a very good case to apply them.

Moreover, any clever way has its drawbacks. In particular, the method proposed by Sergey Nudnov performs insert requests. This means that if the search string is not found in the table, this query will add it. What, in principle, should not be.

In fact, of course, there is a way to correctly make all updates in one request without unwanted inserts. The request for this will be completely furious . And, as I said, no downright terrible need to perform all updates with a single request. A dozen queries in the loop, especially with the use of prepared expressions, especially wrapped in a transaction, have not yet brought down the system to anyone.

  • php.net/manual/ru/pdo.prepared-statements.php Are you talking about these prepared expressions? - web2k17
  • Yes, it is about these. Basically, it also supports mysqli, but it is so inconvenient that it is better to do it immediately via PDO - Ipatiev
  • @And PDO - this is not only the "many drivers". This is almost a complete wrapper for working with a database with many useful functions. Recommend: phpdelusions.net/pdo - Ipatiev

Prepare the data in a table and use INSERT INTO :

 INSERT INTO Users (id, phone, name, surname, second_name, manager_comment, delivery_adress, delivery_time, delivery_date, courier) VALUES ('1', '416-000-0000', 'Vasily', 'Pupkin', 'Michael', 'No comments', '123 Street', '10:00', '1 apr', 'Быстрый'), ('5', '416-000-0005', 'Сергей', 'Нуднов', 'Михайлович', 'No comments', '777 Avenue', '18:00', '1 apr', 'Супер Быстрый'), ('2', '416-000-0001', 'Ivan', 'Ivanov', 'Ivanovich', 'No comments', '345 Street', '11:00', '1 apr', 'Быстрый') ON DUPLICATE KEY UPDATE phone=VALUES(phone), name=VALUES(name), surname=VALUES(surname), second_name=VALUES(second_name), manager_comment=VALUES(manager_comment), delivery_adress=VALUES(delivery_adress), delivery_time=VALUES(delivery_time), delivery_date=VALUES(delivery_date), courier=VALUES(courier) 

It is understood that id is Primary key ! Otherwise it will not work.

  • This is a very bad decision. The author should not insert lines, but update them. This means that if the required row is not found in the table, it is not necessary to insert a row. - Ipatiev
  • @ Ipatiev, The solution is productive in the case of non-dynamic investments. - And
  • @And is a time bomb that, if exploded, will not seem like a little. The ON DUPLICATE query can be used for insertion, but not for an update. - Ipatiev