insert multi rows - (arrays)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
csoreto
Forum Newbie
Posts: 3
Joined: Wed Apr 02, 2008 11:32 am

insert multi rows - (arrays)

Post by csoreto »

Hi,

I have a shopping cart confirmation page with this array - <?php echo $my_shopping_cart->product_detail[$i][0]['f_products_name']; ?>; first of all as the code comes from an PHP class (phpclasses.org) I do not know what means [0]; secondly how can I insert (in a MySQL table) the values of this array in one single query?

carlos soreto
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: insert multi rows - (arrays)

Post by aceconcepts »

I do believe that [0] is an index of an array - so basically [0] would be the very first item in that array as an array starts from 0 not 1.

In terms of adding multiple values into a database all you would need to do is loop your array - foreach or for etc...
csoreto
Forum Newbie
Posts: 3
Joined: Wed Apr 02, 2008 11:32 am

Re: insert multi rows - (arrays)

Post by csoreto »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Thank you.

I have tried for, but
my table continues empty.

##
code:

Code: Select all

 
$editFormAction = $_SERVER['PHP_SELF'];
or($i=0;$i<$my_shopping_cart->num_of_products_id;$i++){
$sql1=sprintf("INSERT INTO test_mysql (f_products_name) VALUES (%)",
    GetSQLValueString($my_shopping_cart->product_detail[$i][0]['f_products_name'], "text"));
    
    mysql_select_db($database_test, $test);
    $result1=mysql_query($sql1, test) or die(mysql_error());
  mysql_insert_id();
}
header("location:something.php");
##

With for($i=0;$i<$my_shopping_cart->num_of_products_id;$i++){
the page does not redirect.

carlos soreto


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
csoreto
Forum Newbie
Posts: 3
Joined: Wed Apr 02, 2008 11:32 am

Re: insert multi rows - (arrays) - SOLVED

Post by csoreto »

Solved!

As 'aceconcepts' wrote the solution is to make a loop (for, etc.)...
The problem was in the database itself...

carlos soreto
Last edited by csoreto on Mon Apr 07, 2008 6:23 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: insert multi rows - (arrays)

Post by John Cartwright »

would you mind posting what you did to solve it, to benefit anyone else that comes along :)

p.s. please edit your original post's title to include [SOLVED]
Post Reply