I'm trying to write an INSERT SQL request to populate a table of my db with a multidimensional array.
The array use the session variables (it's a shopping cart) $_SESSION['panier'], and currently has the following content :
array (size=3)
'ARTCOD' =>
array (size=2)
0 => string 'NA1818BLCFEZ' (length=12)
1 => string 'SER5151BLCFEZ' (length=13)
'COLLIB' =>
array (size=2)
0 => string 'blanc' (length=5)
1 => string 'blanc' (length=5)
'quantite' =>
array (size=2)
0 => int 6
1 => int '8'
My table has more field : the 'ID' field, wich need to auto increment.
the 'reference' field, wich contains the reference of the order, I will generate it randmly.
The 'ARTCOD' field, it's the code associated to an article, here it's in $_SESSION['panier']['ARTCOD'].
The 'CLICOD' field, it's the code associated to the client wich is ordering, here it's in $_SESSION['CLICOD'].
the 'quantite' field, the quantity of the ordered artcicle, contained in $_SESSION['panier']['quantite'].
The 'type' field, contain the order type, here it's just a defined word that will not change.
And the 'date' field, contain the timestamp of the order.
So I need help to write a function that will generate my Mysql request without using multiple INSERT.
I've already create this :
$query = "";
for ($i = 0; $i < count($_SESSION['panier']['ARTCOD']); ++$i) {
$query .= 'INSERT INTO commandevalide (id, reference, ARTCOD, CLICOD, quantite, type, date) VALUES ("", "'.$_SESSION['panier']['ARTCOD']['$i'].'", "'.$reference.'", "'.$_SESSION['CLICOD'].'", '.$_SESSION['panier']['quantite']['$i'].', "Location", now());';
}
But I prefere something wich will give just one INSERT request.
I saw that I need to use the implode function and a foreach loop but I did not manage to create something working
If you guys have any suggestion, I'll be glad to hear them :)
Thanks,
Magatsu
mysqli
? Hopefully not the awfulmysql_query
. – tadman Jun 29 '15 at 14:51