473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help in sending php array to mysql table

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

I am trying to send an array from a mysql_fetch_assoc result from table total_orders to table orders_complete. I have the following code:

[php]
<?php include("config.php");$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 date='xx/xx/xx' and time='xx:xx:xx'"; if(!mysql_query($sql)) { user_error("Failed to update row: $newValue", E_USER_NOTICE); }}?>
[/php]

The above code sucessfully updates the row with time=xx:xx:Xx and date=xx/xx/xx but...
When i search the orders_complete table for the new updated row. It only seems to enter into the table the last key of the array and not the FULL array
i.e.
total_orders table echos

12" pizza £12
half lb burger £3.40
side portion of chips

would enter into the database ONLY side portion of chips

if i put a exit(); inbetween the 2 end }} shown at the end of the script
when I do a seach for the row it only shows as entered

12" pizza £12

and nothing else.

I was wondering if somebody could help me in what im doing wrong. it looks like that the while loop when cycled is overwriting each entry hence I can only see 1 value of the array rather than the total array seporated with a ,
Nov 7 '08 #1
24 2612
Dormilich
8,658 Expert Mod 8TB
the problem is the UPDATE statement. as long as date and time are the same (as seems to be) your previous entries will be overwritten (that's what the UPDATE command does). move the mysql_query() out of the while loop.
[PHP]while($row = mysql_fetch_assoc($result))
{
$newValue .= $row['item'] . $row['cost'] . ", ";
}
// remove last comma
$sql = "UPDATE orders_complete SET ord_descr='$newValue ' WHERE date='xx/xx/xx' and time='xx:xx:xx'";[/PHP]
Nov 7 '08 #2
Hi thanks for such a quick responce.

I have removed the comma and placed the sql update statement outside the while loop. Didnt seem to make a difference. The update query entered the last array value. also if you do a [php]print_r($variable);[/php] this also shows the last item in the array. been trying to get my head around this for 2 days now :o(
Nov 7 '08 #3
Dormilich
8,658 Expert Mod 8TB
could you post a var_dump() of your sql (UPDATE) query?

check if you have ".=" in the while loop, otherwise $newValue will always be the last array element.
Nov 7 '08 #4
sure

this is a var_dump of the sql query out of the while loop

string(109) "UPDATE orders_complete SET ord_descr='Hlf lb Chicken burger3.50, ' WHERE date='06/11/08' and time='13:33:53'"

below shows a var_dump of the sql statement within the while loop

string(109) "UPDATE orders_complete SET ord_descr='9 Inch Seafarer Pizza8.00, ' WHERE date='06/11/08' and time='13:33:53'" string(111) "UPDATE orders_complete SET ord_descr='9 Inch Beef Eater Pizza8.00, ' WHERE date='06/11/08' and time='13:33:53'" string(113) "UPDATE orders_complete SET ord_descr='9 Inch Plus Special Pizza8.00, ' WHERE date='06/11/08' and time='13:33:53'" string(124) "UPDATE orders_complete SET ord_descr='Hlf lb Garlic Mushroom Cheese burger3.50, ' WHERE date='06/11/08' and time='13:33:53'" string(109) "UPDATE orders_complete SET ord_descr='Hlf lb Chicken burger3.50, ' WHERE date='06/11/08' and time='13:33:53'"
Nov 7 '08 #5
Dormilich
8,658 Expert Mod 8TB
move the sql statement out of the loop. use the loop only to build the SET string like reply #2.
Nov 7 '08 #6
ok

moved the sql statement out of the link and changed the $newValue within the loop to:
[php]
$newValue = $row['item'] .= $row['cost'];
[/php]

still only inserts the last entry in the array :o(
Nov 7 '08 #7
oh hang on a min, i think u did it :o)
Nov 7 '08 #8
Dormilich
8,658 Expert Mod 8TB
[PHP]$newValue .= $row['item'] . $row['cost'] . ", ";[/PHP]
Nov 7 '08 #9
very strange. it does insert but i get an error message on line 7 when I add the . after the $newValue
[php]
$newValue .= $row['item'] .= $row['cost'];
[/php]

error message receive

PHP Notice: Undefined variable: newValue in C:\Inetpub\wwwroot\takeaway\testinsertitems.php on line 7

although it does insert it into the table. how can I not get this error message to appear?
Nov 7 '08 #10
Dormilich
8,658 Expert Mod 8TB
see post #7, it doesn't make sense to call the .= operator (add following to current value) twice.

add [PHP]$newValue = '';[/PHP] before the loop

PS double (or better triple) check the code you write, you have quite a lot of typos that trouble you
Nov 7 '08 #11
sorry im new to php programming and im not too sure what you mean. If i remove the . after the $newValue it only inserts the last value in the array, if i keek the . after the $newValue it inserts the entire array but displays the error message i posted in the previous post
Nov 7 '08 #12
Dormilich
8,658 Expert Mod 8TB
probably the best would be if you post the script (or the relevant part) it's hard to guess the errors from one line of code.
Nov 7 '08 #13
wow its worked, array fully entered into the table with no errors. Your a star :o)

sorry im new to php ;o)
Nov 7 '08 #14
Dormilich
8,658 Expert Mod 8TB
no, I'm a god (though xml god) - nah, joking.
to be honest, it was more of a sql understanding and "find the typo" problem

congratulations that you finally made it
Nov 7 '08 #15
i added a ',' at the end of the $newValue so that each entry into the table field would be seporated with a , as shown as :

[php]
$newValue .= $row['item'] .= $row['cost'].=',';
[/php]

My next stage would be then to retreive the string as a whole and use the explode function to seporate each entry via a comma.

would it be something like

[php]
explode(',', $row['item']);
[/php]

cut a long story short I have been asked to create a web based internal ordering system for a local Pizza delivery company. the total_orders table is a temporary table which holds exactly what the customer would like. then using the code you have helped me with, it will then be inserted into the orders_complete table for later reference, so for example if the casher needs to re print the customer receipt or the kitchen check they can do.
Nov 7 '08 #16
Dormilich
8,658 Expert Mod 8TB
you make me curious. I like to have a look at the complete code, there seems room for improvement (or comments). you could also PM the code to me.
Nov 7 '08 #17
I bet your right, as im new i bet i could probily cut half the code down to achully what i am using. ive been working on it for nearly 2 months now, and have learnt a lot, well not enough to overcome your help with was greatfully received :o) I dont mind, i think you would probily laugh at the amount of if and else if statements im using,LOL.

I would like to add you to my Buddy's list of thats ok with you? how would you want me to pass the full code to you?
Nov 7 '08 #18
Dormilich
8,658 Expert Mod 8TB
I would like to add you to my Buddy's list of thats ok with you? how would you want me to pass the full code to you?
one - that ok with me
two - do as you desire, you can contact me via my member details (click on the name...)
i think you would probily laugh at the amount of if and else if statements im using,LOL.
I bet you'd laugh about my code, I had written after learning php for 2 months, too (so would I now)
Nov 7 '08 #19
ha, you should see my database.sql text for creating the tables LOL

theres about 15 tables or so.
Nov 7 '08 #20
so, whats your thoughts on retreiving the array and seporating each line using the comma. do you think the explode function is the best way forward for this?
Nov 7 '08 #21
Dormilich
8,658 Expert Mod 8TB
can't say that without the full code. there is always (nearly) more than one way to solve a problem.
Nov 7 '08 #22
ok i will get the code to you tomorrow if thats ok? getting a little late now :o(

thank you again for helping me out so rapidly. You should be in a programming job :o)
Nov 7 '08 #23
Dormilich
8,658 Expert Mod 8TB
ok i will get the code to you tomorrow if thats ok? getting a little late now :o(
that leaves me now some time for watching some more slayers episodes
thank you again for helping me out so rapidly. You should be in a programming job :o)
sometimes i wish it would

one last question for today: is the pizza good?
Nov 7 '08 #24
LOL yes, definatly haha its the best pizza in town :o)

hehe

if you dont mind, tomorrow I will post you the total.php

basically im having problems in removing a single row from the order_total table
Nov 8 '08 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
6
by: DavidPr | last post by:
I made a little picture gallery for my website. The images and thumbnails are stored on the server and the information in a database. I know how to delete the information from the databasem, but how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.