【问题标题】:PHP LOOP from database and save into one field来自数据库的 PHP LOOP 并保存到一个字段中
【发布时间】:2014-09-26 10:00:17
【问题描述】:

我需要一些帮助。

我有带字段代码的表一和带字段 code_two 的表二。

table_one 中的记录:

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

并像这样发送到 table_two

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

我想将表 one 字段“code”中的记录发送到表 two 字段 code_two。

这里是我的php循环输入框

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

查询中的循环是否有效?我试过了,得到了错误,请帮忙。

【问题讨论】:

  • 您可以将第一个查询中的所有值放入一个数组中并使用implode() 函数,这样您只需要运行一次插入。

标签: php arrays loops field


【解决方案1】:

不需要使用 PHP,你可以在 MySQL 中使用 GROUP_CONCAT() 来完成

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

更多关于http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

【讨论】:

  • 简单的解决方案是最好的 :) 我的朋友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多