Page 1 of 1

How do I Insert array into specific table columns

Posted: Sun Jul 12, 2009 1:57 am
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...

Re: How do I Insert array into specific table columns

Posted: Sun Jul 12, 2009 3:36 am
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.