Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
What database driver are you using? PDO? mysqli? Hopefully not the awful mysql_query. – tadman Jun 29 '15 at 14:51
    
And your question is...? – Marc B Jun 29 '15 at 14:52
    
@tadman : I use Mysqli. – Magatsu Jun 29 '15 at 15:02
    
@Marc B : I edited my post, I just need help to write the function wich will generate the SQL request without using multiple INSERT – Magatsu Jun 29 '15 at 15:05
1  
Can you give an example of some code using multiple insert? This will help us in determining how to optimize your code. – Dave Chen Jun 29 '15 at 15:16
up vote 0 down vote accepted

To combine all those inserts into a single one, you can try this :

$query = "INSERT INTO commandevalide (id, reference, ARTCOD, CLICOD, quantite, type, date) VALUES ";

for ($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.

share|improve this answer
    
Great idea, that's working like a charm ! As we say here in france "Merci !" :) – Magatsu Jun 29 '15 at 17:15
    
Hé ben de rien ^^ – Brewal Jun 29 '15 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.