Mysql INSERT with multidimensional php array -
i'm trying write insert sql request populate table of db multidimensional array.
the array use session variables (it's shopping cart) $_session['panier'], , has 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 : 'id' field, wich need auto increment.
the 'reference' field, wich contains reference of order, generate randmly.
the 'artcod' field, it's code associated article, here it's in $_session['panier']['artcod'].
the 'clicod' field, it's code associated client wich ordering, here it's in $_session['clicod'].
the 'quantite' field, quantity of ordered artcicle, contained in $_session['panier']['quantite'].
the 'type' field, contain order type, here it's defined word not change.
and 'date' field, contain timestamp of order.
so need write function generate mysql request without using multiple insert.
i've create :
$query = ""; ($i = 0; $i < count($_session['panier']['artcod']); ++$i) { $query .= 'insert commandevalide (id, reference, artcod, clicod, quantite, type, date) values ("", "'.$_session['panier']['artcod']['$i'].'", "'.$reference.'", "'.$_session['clicod'].'", '.$_session['panier']['quantite']['$i'].', "location", now());'; }
but prefere wich give 1 insert request.
i saw need use implode function , foreach loop did not manage create working
if guys have suggestion, i'll glad hear them :)
thanks,
magatsu
to combine inserts single one, can try :
$query = "insert commandevalide (id, reference, artcod, clicod, quantite, type, date) values "; ($i = 0; $i < count($_session['panier']['artcod']); ++$i) { $query .= '("", "'.$_session['panier']['artcod'][$i].'", "'.$reference.'", "'.$_session['clicod'].'", '.$_session['panier']['quantite'][$i].', "location", now()), '; } $query = rtrim($query, ', ');
note : don't put $i
between single quotes.
Comments
Post a Comment