1

I'm working on a script which help me to get informations about new books from a website. I have 8 informations (title, author, editor...) to insert, and on each page, there is ten or less books to scan.

The final step is the query, but I have some problems to understand multidimensionnal array.

My prepare syntax :

$req = $db->prepare('INSERT INTO listing(id, Ref, Nom, Auteur, Editeur, Prix, EAN, Small_desc, Dispo) VALUES(\'\', :Ref, :Nom, :Auteur, :Editeur, :Prix, :EAN, :Small_desc, :Dispo)');

And the query... I've made the test with the first line. That's works ! But I Wish to insert all the results.

$req->execute(array('Ref'=>$sortie4[1][0], 'Nom'=>$sortie[1][0], 'Auteur'=>$sortie2[1][0], 'Editeur'=>$sortie3[1][0], 'Prix'=>$sortie6[1][0], 'EAN'=>$sortie5[1][0], 'Small_desc'=>$sortie7[1][0], 'Dispo'=>$sortie8[1][0]));

I've found some examples about multidimensionnal array, but I don't understand how I could use them.

Thanks for any help

  • You should use Transactions and execute the statement for each item in your array. Check out e.g. [this question](http://stackoverflow.com/questions/2708237/php-mysql-transactions-examples). – webmaster777 Jul 05 '13 at 14:02
  • 1
    Is the second row in `[2][0]` or `[1][1]`? – MrCode Jul 05 '13 at 14:04
  • You appear to have multiple multi dimensional arrays, but from your description you seem to be saying all the details are stored in a single multi dimensional array. – Kickstart Jul 05 '13 at 14:07
  • The second row is [1][1] the third [1][2].. and for each variable "sortie". – Florent Delage Jul 05 '13 at 14:11

2 Answers2

0

You can read THIS to learn more about arrays.

  • That's the first page I have try to understand. But, I didn't see an example which can help me. – Florent Delage Jul 05 '13 at 14:48
  • I don't think so. Try this in your code: `$array = array("foo", "bar", "hallo", "world"); var_dump($array);` If you are beginner`try to understand the simpler examples and then you can move on little complicating examples. I think this link which I post has what you need. –  Jul 05 '13 at 15:01
  • Also you can always use var_dump() to see what your array looks like. var_dump() is great function for code debugging. –  Jul 05 '13 at 15:02
0

It can be done like this:

$req = $db->prepare('INSERT INTO listing(id, Ref, Nom, Auteur, Editeur, Prix, EAN, Small_desc, Dispo) VALUES(\'\', :Ref, :Nom, :Auteur, :Editeur, :Prix, :EAN, :Small_desc, :Dispo)');

foreach($sortie4[1] as $key => $value)
{
    $req->execute(array(':Ref'=>$sortie4[1][$key], ':Nom'=>$sortie[1][$key], ':Auteur'=>$sortie2[1][$key], ':Editeur'=>$sortie3[1][$key], ':Prix'=>$sortie6[1][$key], ':EAN'=>$sortie5[1][$key], ':Small_desc'=>$sortie7[1][$key], ':Dispo'=>$sortie8[1][$key]));
}

Changes are:

  • Added foreach so that the statement is prepared once, then execute() looped for each row to insert. By capturing the key, you can access the relevent element in your other arrays.
  • Your parameter names didn't have : prefix in the execute call, I added those.

Side note: your array isn't in the most ideal structure. You have several multidimensional arrays, one for each column. Instead you should only need one for example:

$arr = array(
    array('Ref' => '...', 'id' => '...', 'Nom' => '...'),
    array('Ref' => '...', 'id' => '...', 'Nom' => '...'),
    array('Ref' => '...', 'id' => '...', 'Nom' => '...'),
    array('Ref' => '...', 'id' => '...', 'Nom' => '...')
);
MrCode
  • 63,975
  • 10
  • 90
  • 112