Ignatius Samuel Megis
Ignatius Samuel Megis

Reputation: 185

PHP LOOP from database and save into one field

I need some help.

I have table one with field code and table two with field code_two.

record from table_one :

----code----
32
23
34
25
------------

and send to table_two like this

----code_two-----
32,23,34,25.
-----------------

I want to send record from table one field 'code' into table two field code_two.

Here my php loop input box

foreach($_POST['code'] as $cnt => $qty) {|
    mysql_query("insert into table_two values('$code_from_table_one');
}

is that loop in query is work? I tried and getting error, little help please.

Upvotes: 1

Views: 182

Answers (1)

Robert
Robert

Reputation: 20286

Don't need to use PHP for it you can do it with GROUP_CONCAT() in MySQL

INSERT INTO code_two SELECT GROUP_CONCAT(`code` SEPARATOR ',') FROM code

More on http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Upvotes: 2

Related Questions