Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Closed Thread
How do I insert an Array into a MySQL Database?
Old 07-25-2005, 09:24 AM How do I insert an Array into a MySQL Database?
Skilled Talker

Posts: 58
Trades: 0
Hi,

I have an array with a few entries. How do I insert those entries into a MySQL database?

I have used the foreach () statement, and if I 'echo' the variable inside the foreach statement I can see each value.

BUT when I use the MySQL Query to insert it into the database it only inserts the last item in my Array.

Can anyone please help me with this? It is rather urgent, my boss is sitting on my neck to get this done!
Llama is offline
View Public Profile
 
 
Register now for full access!
Old 07-25-2005, 09:38 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Use the implode and explode functions available in PHP. implode takes an array, and will return a string with all the items in a list. Explode takes a list of items in a string, and gives you back an array. You specify the character(s) that are used as the dividers. For example:

PHP Code:
$test = Array('one','two','five','seven');
foreach(
$test as $value) echo $value.'<br />';
$tstring implode(',' $test);
echo 
$tstring.'<br />';
$test2 explode(',' $tstring);
foreach(
$test2 as $value) echo $value.'<br />'
Will produce:
Code:
one
two
five
seven
one,two,five,seven
one
two
five
seven
The value of $tstring is just a string, and can be inserted into a mysql field of type VARCHAR or TEXT. Convert the array in question to a string for storage, and then recover the original array using explode.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
View Public Profile Visit 0beron's homepage!
 
Old 07-25-2005, 09:43 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Llama,

Use the implode() method to convert your array into a delimited string, then use explode() to reverse the process and convert the concatenated string back into an array.

Follow the links to see code examples.

HTH.

EDIT: Ah, 0beron is quick on the draw! Oh well, consider it confirmation!
__________________
—Kyrnt
Kyrnt is offline
View Public Profile Visit Kyrnt's homepage!
 
Old 07-25-2005, 10:35 AM Thank You!
Skilled Talker

Posts: 58
Trades: 0
Thank you very much! I'll try it tonight. Thanx again!
Llama is offline
View Public Profile
 
Old 07-25-2005, 10:38 AM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Trades: 0
I thought it was urgent and your boss was chopin @ ur *$*$ to get it sorted? lol

asm is offline
View Public Profile Visit asm's homepage!
 
Old 07-25-2005, 02:00 PM
Skilled Talker

Posts: 58
Trades: 0
Well, i stay in South Africa and here it is 8pm now, and still working on it, hope to have it all done by Friday. I tried your advise, and yes it inserts all the data now, BUT.... it inserts it all into one field in the database. Each Array position should be a new line entry in the database. Maybe I should explain what i am required to build. Our company manufacture parts for companies accross the world, and they need a program that basically automates their shipping for one department. so it is basically a shipping inventory list if you want to call it that. Each line that is entered in the PHP Form contains several fields that should be entered into the database, but each Line on the form is also a new line in the database. Does that explain what i am trying a little better?

I really do appreciate ALL your help though, because I have downloaded so many books on PHP and I couldn't really get it right, but NOTHING beats asking someone!! :-)


Thanks again.
Llama is offline
View Public Profile
 
Old 07-25-2005, 02:16 PM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Trades: 0
i am only joking dude. Although slightly more technical, the code above still applies. Instead of echo you INSERT
Is that any clearer?
asm is offline
View Public Profile Visit asm's homepage!
 
Old 07-25-2005, 02:55 PM
Skilled Talker

Posts: 58
Trades: 0
Dont worry Im joking to...

Well rather let me give you the script that I've got ... PLEASE DONT LAUGH!! I'm fairly new to the whole PHP thing....

<?php
$shipment_base_invoice=$_POST['shipment_base_invoice];

foreach ($shipment_base_invoice as $value) echo ($value.'<br>');
$tstring = implode(',' , $shipment_base_invoice);

$shipment_base_invoice_query="INSERT INTO boeing_shipmenttbl (shipment_base_invoice) VALUES ('$tstring')";

$shipment_base_invoice_result="mysql_query ($shipment_base_invoice_query);
if ($shipment_base_invoice_result)
{
echo ("RECORDED!")l
exit();
}
?>


So.... does that help in anyway? Cause all that happens is that it inserts the whole Array into one field of my Database table, where each value must be entered into a new line.

Can you help me?
Llama is offline
View Public Profile
 
Old 07-25-2005, 03:21 PM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Trades: 0
//put connection to database here

foreach($shipment_base_invoice as $value)
{
$insert="INSERT INTO boeing_shipmenttbl (shipment_base_invoice) VALUES ('$value')";

mysql_query($insert) OR die(mysql_error())

}

you could give that a try

the line $shipment_base_invoice=$_POST['shipment_base_invoice];
has a ' missing POST['shipment_base_invoice'];
asm is offline
View Public Profile Visit asm's homepage!
 
Old 07-25-2005, 03:35 PM
Skilled Talker

Posts: 58
Trades: 0
You Are A Genius!!
That works... now I just need to put things together and Phase one of this project will be completed.

Thank you very much for your help!! There is still some other questions I have but I will do that once I have finnished with setting up all the information to be entered into the database.

Thank You!!
Llama is offline
View Public Profile
 
Old 07-25-2005, 04:04 PM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Trades: 0
glad i could help
asm is offline
View Public Profile Visit asm's homepage!
 
Old 08-01-2005, 10:43 AM
Skilled Talker

Posts: 58
Trades: 0
Hi ASM!!

Llama here... I was so eager to see your reply to my little problem with inserting Array's into MySQL Database... and when I saw you posted a reply, that I HOPE could help me... I could not access it anymore! Now when I go to the query I logged it shows that there is only 10 replies and also only 57 views. Last I know of there was 17 replies and over a 100 views... PLEASE tell me you have a solution for my little problem!??
Llama is offline
View Public Profile
 
Old 08-01-2005, 12:31 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Quote:
Originally Posted by Llama
Now when I go to the query I logged it shows that there is only 10 replies and also only 57 views. Last I know of there was 17 replies and over a 100 views...
Llama, the reason for the alteration to the thread was the database problems WT experienced over the weekend which precipitated the need to roll the entire WT forum back to July 26th. Sorry for any inconvenience this may have caused.
__________________
—Kyrnt
Kyrnt is offline
View Public Profile Visit Kyrnt's homepage!
 
Old 08-02-2005, 04:58 AM
Skilled Talker

Posts: 58
Trades: 0
Kyrnt,
Thats ok... :-) it happens.
Llama is offline
View Public Profile
 
Old 08-03-2005, 01:06 PM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Trades: 0
has this been resolved??
asm is offline
View Public Profile Visit asm's homepage!
 
Old 08-03-2005, 01:30 PM
Skilled Talker

Posts: 58
Trades: 0
I feel like CRYING!! Or KILLING!!! :-) just kidding... but no, I am still strugeling my @#% oFF!!!

I changed the whole thing to use UPDATE instead of INSERT, but here's the thing:

COLUMN 1 COLUMN 2 COLUMN 3
-------------------------------------------------------------------------------------
row 1 fixed value value=always the same value=changes
row 2 fixed value value=always the same value=changes
row 3 fixed value value=always the same value=changes


I have an array with 3 values, 1 for each row.
When I update the mysql table, it will update COLUMN 3 as it should BUT it inserts the LAST value of the aray into all 3 rows.

HOW do I insert each value in its own row ????


PLEASE HELP!!!

I am getting GRILLED by my boss!!
Llama is offline
View Public Profile
 
Old 08-03-2005, 01:35 PM
asm
Extreme Talker

Posts: 217
Location: UK.Lancashire(true)
Trades: 0
Do you mean the last value of the array i.e. $myArray[2] is being inserted, are you updating old data or inserting fresh data?
asm is offline
View Public Profile Visit asm's homepage!
 
Old 08-03-2005, 01:47 PM
Skilled Talker

Posts: 58
Trades: 0
there are 3 values in the array lets say: blue, green, red.
It should insert blue into Column 3 row 1
green into Column 3 row 2
red into Cloumn 3 row 3

BUT IT DOES THIS INSTEAD:
red into column 3 row 1
red into column 3 row 2
red into column 3 row 3

Yes, it is new data.
Llama is offline
View Public Profile
 
Old 08-04-2005, 09:18 AM
Skilled Talker

Posts: 58
Trades: 0
I am using the foreach statement...
ANY ADVISE?>??
Llama is offline
View Public Profile
 
Closed Thread     « Reply to How do I insert an Array into a MySQL Database?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53854 seconds with 11 queries