Hi All

I was wondering if somebody could help me as this is driving me crazy.

I am trying to send an array from a mysql_fetch_array result to a seporate table. I have the following code:


$result7 = mysql_query("SELECT * from order_total");
while($row = mysql_fetch_array($result7)) 
$test = array($row['item'].' '.$row['cost'].',');
foreach($test as $value) {
//$arr = array($value[0].$value[1].',');

echo $value;
mysql_query("UPDATE orders_complete SET ord_descr='$value' WHERE date='06/11/08' and time='13:33:53'") 
or die(mysql_error());

the table currently holds customer orders i.e

9 Inch Seafarer Pizza £8.00
9 Inch Beef Eater Pizza £8.00
9 Inch Plus Special Pizza £8.00
15 Inch Vegetarian Pizza £12.00


echo $value

seems to echo ok the following to the screen:

9 Inch Seafarer Pizza 8.00,9 Inch Beef Eater Pizza 8.00,9 Inch Plus Special Pizza 8.00,15 Inch Vegetarian Pizza 12.00,

but when viewing the ammendments made to the row, it ONLY inserts the last record: in this case 15 Inch Vegetarian Pizza £12.00

I was wondering if anybody could help me understand what i am doing wrong?

Recommended Answers

All 4 Replies

I think you may want to do something more like:

// Assuming there is a open mysql connection
$sql = "SELECT item, cost FROM orders_total";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
  $newValue = $row['item'] .", ". $row['cost'];
  $sql = "UPDATE orders_complete SET ord_descr='$newValue ' WHERE [...]";
  if(!mysql_query($sql)) {
    user_error("Failed to update row: $newValue", E_USER_NOTICE);

thanks for that,

I will impliment that as soon as I can and let you know how I get on. As Im a new PHP programmer im not too sure what the difference is between the mysql_fetch_array and the mysql_fetch_assoc functions although I have looked it up on the php.net website it doesnt really tell me in dummie language.
thanks again

hi again

unfortunitly using the mysql_fetch_assoc didnt resolve the issue. when viewing the row changed by the update query only inserted the last row of the select item,cost from orders_total table

for example:

9 Inch Seafarer Pizza 8.00, 9 Inch Beef Eater Pizza 8.00, 9 Inch Plus Special Pizza 8.00, Hlf lb Garlic Mushroom Cheese burger 3.50, Hlf lb Chicken burger 3.50,

only updates into the orders_complete table the following rather than the total array:

Hlf lb Chicken burger 3.50,

if i put an


inbetween the two }} at the end it seems to insert the firstline of the array into the table orders_complete. I think what its doing is everytime the while loop cycles its writing to the table sucessfully but not remebering the previous keys in the array hence the reason for it only inserting the last array. I think i need a foreach or a for loop to do something else with the code but im not too sure

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.