How do I Insert array into specific table columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

How do I Insert array into specific table columns

Post by varma457 »

Hi,

I had problem with inserting array values into table specific columns.Like

$abc=(asfh,adsf,afdaf);

I want to insert these three elements in a table in a different columns in a single loop..

foreach($content as $value)
{
$insert="INSERT INTO test(username,passowrd) VALUES ('$value')";

mysql_query($insert);
}


I tried above code it is not working...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I Insert array into specific table columns

Post by requinix »

varma457 wrote:I want to insert these three elements in a table in a different columns in a single loop..
No, you don't. You actually want to insert those three elements into a table in one query. Assuming, that is, that you aren't talking about three separate rows in the table each with only one bit of data in only one column.

You can do

Code: Select all

INSERT INTO table (field1, field2, field3) VALUES (data1, data2, data3)
or

Code: Select all

INSERT INTO table SET field1=data1, field2=data2, field3=data3
However I don't think you said what you want. It seems more like you want to insert three rows of data at once, in which case you use

Code: Select all

INSERT INTO table (fields) VALUES (first set of values), (second set of values), (third set of values)
It's also funny (not really) that your SQL doesn't look anything like the "three elements in a table in a different columns" you claim you need.
Post Reply