-1

追加/削除ボタンをクリックして新しい行を動的に追加/削除できるテーブルを含むフォームがあります。

私の問題は、新しく追加/削除された行をデータベースに保存しようとしたときに始まります。jQuery の Ajax を使用してデータを保存できたので、post var を使用せず、各入力のすべてのデータを「収集」しています。これらすべてのデータを取得し、それらを保存している store.php に送信することができました。

私の質問は、本当に少ないクエリで変更を保存するにはどうすればよいかということです。つまり、これは store.php が私の mysql サーバーに送信している出力です。

UPDATE transport SET transporttype = "3" WHERE id = 1
UPDATE transport SET transporttype = "2" WHERE id = 6
UPDATE transport SET transporttype = "1" WHERE id = 7
UPDATE transport SET transportmandataire = "1" WHERE id = 1
UPDATE transport SET transportmandataire = "3" WHERE id = 6
UPDATE transport SET transportmandataire = "3" WHERE id = 7
UPDATE transport SET transportexpediteur = "1" WHERE id = 1
UPDATE transport SET transportexpediteur = "3" WHERE id = 6
UPDATE transport SET transportexpediteur = "3" WHERE id = 7
UPDATE transport SET transportdestinataire = "1" WHERE id = 1
UPDATE transport SET transportdestinataire = "2" WHERE id = 6
UPDATE transport SET transportdestinataire = "4" WHERE id = 7
UPDATE transport SET transporttransporteur = "5" WHERE id = 1
UPDATE transport SET transporttransporteur = "4" WHERE id = 6
UPDATE transport SET transporttransporteur = "3" WHERE id = 7
UPDATE transport SET transportbondetransportnumerodesuivi = "1563123" WHERE id = 1
UPDATE transport SET transportbondetransportnumerodesuivi = "23544" WHERE id = 6
UPDATE transport SET transportbondetransportnumerodesuivi = "23544" WHERE id = 7
UPDATE transport SET transportlivraisonprevisiondate = "1" WHERE id = 1
UPDATE transport SET transportlivraisonprevisiondate = "1" WHERE id = 6
UPDATE transport SET transportlivraisonprevisiondate = "1" WHERE id = 7
UPDATE transport SET transportlivraisonconstatdate = "test" WHERE id = 1
UPDATE transport SET transportlivraisonconstatdate = "test" WHERE id = 6
UPDATE transport SET transportlivraisonconstatdate = "test" WHERE id = 7
UPDATE transport SET transportlivraisonconstatheure = "test" WHERE id = 1
UPDATE transport SET transportlivraisonconstatheure = "test" WHERE id = 6
UPDATE transport SET transportlivraisonconstatheure = "test" WHERE id = 7
UPDATE transport SET transportlivraisonprevisiondate = "1" WHERE id = 1
UPDATE transport SET transportlivraisonprevisiondate = "1" WHERE id = 6
UPDATE transport SET transportlivraisonprevisiondate = "1" WHERE id = 7
UPDATE transport SET transportlivraisonprevisionheure = "1" WHERE id = 1
UPDATE transport SET transportlivraisonprevisionheure = "1" WHERE id = 6
UPDATE transport SET transportlivraisonprevisionheure = "1" WHERE id = 7
UPDATE transport SET transportetat = "6" WHERE id = 1
UPDATE transport SET transportetat = "1" WHERE id = 6
UPDATE transport SET transportetat = "6" WHERE id = 7
UPDATE transport SET transportimperatif = "09/17/2012" WHERE id = 1
UPDATE transport SET transportimperatif = "09/18/2012" WHERE id = 6
UPDATE transport SET transportimperatif = "09/09/2012" WHERE id = 7
UPDATE transport SET transportbondetransportmoyendetransmission = "2" WHERE id = 1
UPDATE transport SET transportbondetransportmoyendetransmission = "2" WHERE id = 6
UPDATE transport SET transportbondetransportmoyendetransmission = "1" WHERE id = 7
UPDATE transport SET transportbondetransportassurance = "86" WHERE id = 1
UPDATE transport SET transportbondetransportassurance = "86" WHERE id = 6
UPDATE transport SET transportbondetransportassurance = "86" WHERE id = 7
UPDATE transport SET transportobservation = "Aucune observation disponible." WHERE id = 1
UPDATE transport SET transportobservation = "Aucune observation disponible." WHERE id = 6
UPDATE transport SET transportobservation = "Aucune observation disponible." WHERE id = 7
UPDATE transport SET transportreclamationmotif = "3" WHERE id = 1
UPDATE transport SET transportreclamationmotif = "4" WHERE id = 6
UPDATE transport SET transportreclamationmotif = "1" WHERE id = 7
UPDATE transport SET transportreclamationnumerosuivis = "B87GT9" WHERE id = 1
UPDATE transport SET transportreclamationnumerosuivis = "656786" WHERE id = 6
UPDATE transport SET transportreclamationnumerosuivis = "C89GF8" WHERE id = 7
UPDATE transport SET transportreclamationdate = "09/17/2012" WHERE id = 1
UPDATE transport SET transportreclamationdate = "09/10/2012" WHERE id = 6
UPDATE transport SET transportreclamationdate = "09/24/2012" WHERE id = 7
UPDATE transport SET transportreclameetat = "2" WHERE id = 1
UPDATE transport SET transportreclameetat = "1" WHERE id = 6
UPDATE transport SET transportreclameetat = "1" WHERE id = 7
UPDATE transport SET transportreclamationrelancedate = "09/19/2012" WHERE id = 1
UPDATE transport SET transportreclamationrelancedate = "09/04/2012" WHERE id = 6
UPDATE transport SET transportreclamationrelancedate = "09/17/2012" WHERE id = 7
UPDATE transport SET transportreclamationrelancemoyen = "1" WHERE id = 1
UPDATE transport SET transportreclamationrelancemoyen = "1" WHERE id = 6
UPDATE transport SET transportreclamationrelancemoyen = "1" WHERE id = 7
UPDATE transport SET transportindemnisationdate = "09/06/2012" WHERE id = 1
UPDATE transport SET transportindemnisationdate = "09/19/2012" WHERE id = 6
UPDATE transport SET transportindemnisationdate = "09/11/2012" WHERE id = 7
UPDATE transport SET transportindemnisationmontant = "600" WHERE id = 1
UPDATE transport SET transportindemnisationmontant = "600 €" WHERE id = 6
UPDATE transport SET transportindemnisationmontant = "600" WHERE id = 7
UPDATE transport SET transportindemnisationmoyen = "1" WHERE id = 1
UPDATE transport SET transportindemnisationmoyen = "1" WHERE id = 6
UPDATE transport SET transportindemnisationmoyen = "1" WHERE id = 7
UPDATE transport SET transportreclamationobservation = "Non pas d'observation" WHERE id = 1
UPDATE transport SET transportreclamationobservation = "non plus" WHERE id = 6
UPDATE transport SET transportreclamationobservation = "encore moins" WHERE id = 7

すべてのデータを 1 つずつ保存するのに 5 秒ほどかかるため、これは非常に遅いと言う必要はありません。

4

1 に答える 1

1

ON DUPLICATE KEY UPDATEを利用できます。

INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12)
ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);

それが何をするかの簡単な要約:

ON DUPLICATE KEY UPDATE を指定し、UNIQUE インデックスまたは PRIMARY KEY で値が重複する行が挿入された場合、古い行の UPDATE が実行されます。

これらのクエリを PHP スクリプトから実行している場合は、準備済みステートメント (mysqli または PDO) を使用していることを確認する必要があります。通常の mysql 拡張機能を使用している場合は、各クエリでデータベース サーバーへの往復を行うことになります。ただし、準備済みステートメントを使用すると、同様のクエリを 1 つの準備済みステートメントにグループ化し、異なるデータで複数回実行できます。

于 2012-09-21T08:51:03.833 に答える