943,917 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2049
  • PHP RSS
Nov 6th, 2008
0

help in sending php array to mysql table

Expand Post »
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:

php Syntax (Toggle Plain Text)
  1.  
  2. include("config.php");
  3.  
  4. $result7 = mysql_query("SELECT * from order_total");
  5. while($row = mysql_fetch_array($result7))
  6. {
  7. $test = array($row['item'].' '.$row['cost'].',');
  8. foreach($test as $value) {
  9. //$arr = array($value[0].$value[1].',');
  10.  
  11. }
  12. echo $value;
  13. mysql_query("UPDATE orders_complete SET ord_descr='$value' WHERE date='06/11/08' and time='13:33:53'")
  14. or die(mysql_error());
  15. }
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

the
php Syntax (Toggle Plain Text)
  1. 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?
Thanks
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
psycho007 is offline Offline
23 posts
since Oct 2008
Nov 6th, 2008
0

Re: help in sending php array to mysql table

I think you may want to do something more like:
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Assuming there is a open mysql connection
  3. $sql = "SELECT item, cost FROM orders_total";
  4. $result = mysql_query($sql) or die(mysql_error());
  5.  
  6. while($row = mysql_fetch_assoc($result)) {
  7. $newValue = $row['item'] .", ". $row['cost'];
  8. $sql = "UPDATE orders_complete SET ord_descr='$newValue ' WHERE [...]";
  9. if(!mysql_query($sql)) {
  10. user_error("Failed to update row: $newValue", E_USER_NOTICE);
  11. }
  12. }
  13. ?>
Last edited by Atli; Nov 6th, 2008 at 8:19 pm. Reason: Pasted the wrong code.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 7th, 2008
0

Re: help in sending php array to mysql table

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
Reputation Points: 14
Solved Threads: 0
Newbie Poster
psycho007 is offline Offline
23 posts
since Oct 2008
Nov 7th, 2008
0

Re: help in sending php array to mysql table

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,
Reputation Points: 14
Solved Threads: 0
Newbie Poster
psycho007 is offline Offline
23 posts
since Oct 2008
Nov 7th, 2008
0

Re: help in sending php array to mysql table

if i put an
php Syntax (Toggle Plain Text)
  1. exit();
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
Reputation Points: 14
Solved Threads: 0
Newbie Poster
psycho007 is offline Offline
23 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: What is solution to this query
Next Thread in PHP Forum Timeline: what's wrong with my process.php?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC