473,761 Members | 9,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multidimensiona l array in to a MySQL table

Trying to load an multidimensiona l array into a MySQL table with columns as
follows,

level1,level2,l evel3,illust,it em,description, partNo,qua,pric e,remarks,weigh t
,size,mass

the array first line is $input[0][level1]Engine
the array secondline is $input[0][level2]Cylinder Head
etc..

A 'foreach' inside a 'foreach' echo of the array gives the following, which
is correct.

0, level1, Engine
0, level2, Cylinder Head
0, level3,
0, illust, 001.pdf
0, item, 1
0, description, Casting
0, partNo, 238356
0, qua, 1
0, price, 1,245.00
0, remarks, This is for the Z350 series
0, weight, 5
0, size, 5x5x5
0, mass, 37
1, level1, Engine
1, level2, Cylinder Head
1, level3,
1, illust, 001.pdf
1, item, 2
1, description, Valve, inlet
1, partNo, 452790
1, qua, 4
1, price, 5.46
1, remarks, This is for the Z350 series
1, weight, 5
1, size, 5x5x5
1, mass, 37
2, level1, Engine
2, level2, Cylinder Head
2, level3,
2, illust, 001.pdf
2, item, 3
2, description, Valve, exhaust
2, partNo, 345436
2, qua, 4
2, price, 5.99
2, remarks, This is for the Z350 series
2, weight, 5
2, size, 5x5x5
2, mass, 37

etc..

I have used the print_r to see all the data in the array also.
I've tried the extract() function on the first key which I assume is also an
array, inside and outside the loop to get at the column headings.

I've tried allsorts of while and for loops but can't seem to work out how to
get down to the last key to get the following statement to work. I'm trying
the get the array to work through the first key [0] to fill the row, then
the next key [1] to fill the next row in the table etc..

$query2 = "INSERT INTO $tableName
(level1,level2, level3,illust,i tem,description ,partNo,qua,pri ce,remarks,weig h
t,size,mass) VALUES
('$level1','$le vel2','$level3' ,'$illust','$it em','$descripti on','$partNo',' $
qua','$price',' $remarks','$wei ght','$size','$ mass')";
$result = mysql_query($qu ery2) or die("Couldn't insert data.");

If I knock out the loops and have the above as a single statement, I can get
the last row of records into the table.

Hope someone can put a novice out of his misery.

Thanks
Stephen
Mar 14 '06 #1
5 2823
Stephen Preston wrote:
Trying to load an multidimensiona l array into a MySQL table with columns as
follows,

level1,level2,l evel3,illust,it em,description, partNo,qua,pric e,remarks,weigh t
,size,mass

the array first line is $input[0][level1]Engine
the array secondline is $input[0][level2]Cylinder Head
etc..

A 'foreach' inside a 'foreach' echo of the array gives the following, which
is correct.

0, level1, Engine
0, level2, Cylinder Head
0, level3,
0, illust, 001.pdf
0, item, 1
0, description, Casting
0, partNo, 238356
0, qua, 1
0, price, 1,245.00
0, remarks, This is for the Z350 series
0, weight, 5
0, size, 5x5x5
0, mass, 37
1, level1, Engine
1, level2, Cylinder Head
1, level3,
1, illust, 001.pdf
1, item, 2
1, description, Valve, inlet
1, partNo, 452790
1, qua, 4
1, price, 5.46
1, remarks, This is for the Z350 series
1, weight, 5
1, size, 5x5x5
1, mass, 37
2, level1, Engine
2, level2, Cylinder Head
2, level3,
2, illust, 001.pdf
2, item, 3
2, description, Valve, exhaust
2, partNo, 345436
2, qua, 4
2, price, 5.99
2, remarks, This is for the Z350 series
2, weight, 5
2, size, 5x5x5
2, mass, 37

etc..

I have used the print_r to see all the data in the array also.
I've tried the extract() function on the first key which I assume is also an
array, inside and outside the loop to get at the column headings.

I've tried allsorts of while and for loops but can't seem to work out how to
get down to the last key to get the following statement to work. I'm trying
the get the array to work through the first key [0] to fill the row, then
the next key [1] to fill the next row in the table etc..

$query2 = "INSERT INTO $tableName
(level1,level2, level3,illust,i tem,description ,partNo,qua,pri ce,remarks,weig h
t,size,mass) VALUES
('$level1','$le vel2','$level3' ,'$illust','$it em','$descripti on','$partNo',' $
qua','$price',' $remarks','$wei ght','$size','$ mass')";
$result = mysql_query($qu ery2) or die("Couldn't insert data.");

If I knock out the loops and have the above as a single statement, I can get
the last row of records into the table.

Hope someone can put a novice out of his misery.

Thanks
Stephen


Stephen,

How about showing us the failing code? It would be much easier to
troubleshoot what's going wrong.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Mar 14 '06 #2
Message-ID: <aM************ ********@comcas t.com> from Jerry Stuckle
contained the following:
How about showing us the failing code? It would be much easier to
troubleshoot what's going wrong.


And how did the data get into the multi-dimensional array in the first
place?

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 14 '06 #3
Its not, really, its in $table[$row][$col] = $value format, at least if
i understand his post correctly. It also sounds like an interation
problem? I can't rightly tell if its trouble getting the last key from
table ( $row from $table ) or last key from row ($col from each $row).

In either case:

$i = 0; to $i < n, iterates n times. Make sure you're iterating to the
correct amount

foreach($table as $row) {
$columns = implode(... array_keys($row ));
$values = implode(... array_values($r ow));
$query = "insert into table ($columsn) values ($values)";
}

or

foreach($table as $row) {
"insert into table (col1, col2, ...) values ($row[level1], $row[level2]
....)";
}

Hope that helps?

Mar 14 '06 #4
Hi

Thanks to you all for replying, and sorry if my explaination was garbled.
The code for the section is as follows.
The first part empties the table first (which it does). After persevering
further last night the code below empties the contents of the table, and the
$result inside the brackets fills one row of the MySQL table with the first
row of the array, then issues the 'Couldn't insert data'.
If I swap the $result is outside the brackets (# commented out below), the
table is emptied again and then the last row of the array fills one row of
the table. 'Couldn't insert data' is not issued though this time.
if(isset($_POST['dataok']))
{
$input = $_POST[input];
include("dataup .inc");
$query1 = "DELETE FROM $tableName";
$result = mysql_query($qu ery1)
or die("Couldn't delete the data.");
echo "All data within $tableName has been deleted!!!<p>";

foreach($input as $row)
{
$query2 = "INSERT INTO $tableName
(level1,level2, level3,illust,i tem,description ,partNo,qua,pri ce,remarks,weig ht,size,mass)
VALUES
('$row[level1]','$row[level2]','$row[level3]','$row[illust]','$row[item]','$row[description]','$row[partNo]','$row[qua]','$row[price]','$row[remarks]','$row[weight]','$row[size]','$row[mass]')";
$result = mysql_query($qu ery2) or die("Couldn't insert data.");
}
# $result = mysql_query($qu ery2) or die("Couldn't insert
data.");

exit();
} # endif

Still trying to understand Richards first example (my lack of
understanding), not his explaination I think.

Thanks
Stephen
Mar 14 '06 #5
Stephen Preston wrote:
Hi

Thanks to you all for replying, and sorry if my explaination was garbled.
The code for the section is as follows.
The first part empties the table first (which it does). After persevering
further last night the code below empties the contents of the table, and the
$result inside the brackets fills one row of the MySQL table with the first
row of the array, then issues the 'Couldn't insert data'.
If I swap the $result is outside the brackets (# commented out below), the
table is emptied again and then the last row of the array fills one row of
the table. 'Couldn't insert data' is not issued though this time.
if(isset($_POST['dataok']))
{
$input = $_POST[input];
include("dataup .inc");
$query1 = "DELETE FROM $tableName";
$result = mysql_query($qu ery1)
or die("Couldn't delete the data.");
echo "All data within $tableName has been deleted!!!<p>";

foreach($input as $row)
{
$query2 = "INSERT INTO $tableName
(level1,level2, level3,illust,i tem,description ,partNo,qua,pri ce,remarks,weig ht,size,mass)
VALUES
('$row[level1]','$row[level2]','$row[level3]','$row[illust]','$row[item]','$row[description]','$row[partNo]','$row[qua]','$row[price]','$row[remarks]','$row[weight]','$row[size]','$row[mass]')";
$result = mysql_query($qu ery2) or die("Couldn't insert data.");
}
# $result = mysql_query($qu ery2) or die("Couldn't insert
data.");

exit();
} # endif

Still trying to understand Richards first example (my lack of
understanding), not his explaination I think.

Thanks
Stephen


Rather than just saying "Couldn't insert data", you need to find out WHY
it couldn't insert it. Try mysql_error() and see what it says.

I suspect it has to do with your values; amongst other things, numeric
values should not have quotes around them.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Mar 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4902
by: Dave Smithz | last post by:
Hi there. Because of the lack of a Union query in MySQL 3 I have decided to take the approach where I populate two arrays with values from similar tables in DB. In this case they are `courses` and `lessons` Lets say Courses has fields CourseID(PK), Date, Name, RunBy, Status The lessons table is related to the Courses table in that a Course can have
1
1664
by: pauld | last post by:
from a MySQL DB i want to get a multidimensional array that i can loop through either key =field name value = array of ENUM options or array =field name, array= ENUM options and increment x inside loop $q=mysql_query("SHOW FIELDS FROM table" ) or die ("Query failed");
5
1953
by: TheKeith | last post by:
I can't figure out why this doesn't work: --------------------------------------------- greeting = new Array(); greeting = "hey"; greeting = "bye"; trace(greeting + greeting);
10
12213
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
1
1752
by: shailajaAdiga | last post by:
Hi All, there are 4 different categories which each month will bw updated. In each category(source),there are many editions. I have to display 6months updates. its like one is month array which contains 6months' names. source array contains source of that month. $table_row conatins editions of particular source. looking forward to get help from anyone Thanks Here is the code..
1
1756
pradeepjain
by: pradeepjain | last post by:
Hii, i need help in sorting multidimensianal array... i Read the multi sort function cld not use tht so i need help..This is the code. here the problem is tht i need to take out values from 2 different tables.i am getting the output properly but only thing is tht i need to sort the o/p as per name here fid=6 is designation..uid is the id of the user ,fid=3 is Name of the person,fid=11 is for extension ,fid=1 is research...
4
2513
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it would be slower for jsp to handel. Okay here is an example of my multidimensional array (only part of it)(bottom of the post). It works fine I can call any of the 3rd dimension without trouble. Now How would I go about printing something like...
5
5391
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do something like this? var $userArray = array(array());
2
14503
by: mouac01 | last post by:
Let's say I have this MySQL table. Year Region Amount 2007 West 100 2007 East 200 2007 North 300 2007 South 400 2008 West 500 2008 East 600 2008 North 700
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.