Forum Moderators: coopster

Message Too Old, No Replies

php - mysql - insert into problem

         

numnutz

5:33 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



Hi - I am trying to write a simple shopping cart and have a small problem.
I have a loop to get data from a session file.
When I echo the loop out all values appear in the correct order until the end of the file, however if I try to put each row of values in a database I only get the first row.

Here is my code:

$fp = fopen ('sessions/xxw.dat', "r");

while ($data = fgetcsv ($fp, 500)) {

//setup values from array

$item_id = $data[0]; // test for this one below to ensure we don't bring in blank rows
$item_desc = $data[1];
$item_price = $data[2];
$item_qty = $data[3];
$item_postage = $data[4];

if ($item_id) {

mysql_query("INSERT INTO phpcart_items_ordered (order_id,item_id,item_desc,item_price,item_qty,item_postage) VALUES

('$order_id','$item_id','$item_desc','$item_price','$item_qty','$item_postage')");

echo "$order_id $item_id $item_desc $item_price $item_qty $item_postage";
echo "<br />";

}

}

fclose ($fp);

I have been looking at this problem for a couple of days now and my eyes have turned monitor shaped. I must have missed something somewhere.

Note - $order_id is set up earlier in the program

Can anyone help?

Thanks in advance

nn :)

the_nerd

6:04 pm on Aug 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



did you try and insert the other rows manually into your database. Maybe orderid is defined as primary key?

Besides I'm not sure if it's ok to include numerical values in single quotes, but if it's ok for the first row ...

What I'd try is this: echo the complete insert query, copy and paste it to mysql sql-form and see what happens.

numnutz

9:07 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



Yes it was my fault - When I set up the database I made the order_id a primary key. I didn't spot it because I had (mysql) error reporting turned off (or die) - I was just relying on php error reporting.

Sometimes it just doesn't go my way....

Thanks for your input - I suspected something simple.

nn :)