Jump to content


Photo

**SOLVED** Putting an array in database


  • Please log in to reply
2 replies to this topic

#1 masterchiller

masterchiller
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 March 2006 - 09:57 PM

I'm using the following code;

<?php
foreach ($_SESSION['ordered_articles'] AS $array)
{
$articlenumber = $array['artnumber'];
$articleamount = $array['amount'];
}

$mysql_query = "INSERT INTO `orders` values ('NULL', '$articlenumber', '$articleamount')";
$result = mysql_query($mysql_query);
?>

This code puts the array in the database. But when I have more than one article in my array, it just ads one article in the database, instead of them all.

What am I doing wrong here ?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 March 2006 - 10:26 PM

The mysql insert also needs to be inside the foreach loop

foreach ($_SESSION['ordered_articles'] AS $array)
{
    $articlenumber = $array['artnumber'];
    $articleamount = $array['amount'];
    $mysql_query = "INSERT INTO `orders` values ('NULL', '$articlenumber', '$articleamount')";
    $result = mysql_query($mysql_query);
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 masterchiller

masterchiller
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 March 2006 - 10:49 PM

Thank you !! This really worked for me! =)

I was looking for a while loop or something, but this is a much easier and better solution ;).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users