插入批量数据数组?
我想通过 insert_batch
插入以下数据,如数据库表 (mysql) 中的以下示例所示:
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
我想按如下方式插入它们:
应该如何更改php代码和html代码?我该怎么办?
I want insert following data by insert_batch
as in following example in database table (mysql):
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
I want insert they as this:
How should change php code and html code? what do i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您正在使用 CodeIgniter,并且要插入的数据库表的名称称为“units”,并且其“id”列是自动增量的。
我基于 CodeIgniter 用户指南版本 2.0.3 中的解决方案,使用对 Database 类的帮助器
($this->db->insert_string())
的调用。请参阅 http://codeigniter.com/user_guide/database/helpers.html
< Database 类提供的 code>insert_string 函数似乎采用一个关联数组,从内部元素构建一个
INSERT
语句,执行它,然后返回一个数字错误代码。I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.
I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper
($this->db->insert_string())
of the Database class.Refer to http://codeigniter.com/user_guide/database/helpers.html
The
insert_string
function that the Database class provides appears to take an associative array, build anINSERT
statement from the elements inside, execute it and then return a numerical error code.哈哈,它并不漂亮,但有时可能会起作用:
我什至建议您不要使用它。但也许它可能会激励某人提供更好的解决方案。
干杯。
LOL, it's not pretty, but it might work sometimes:
I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.
Cheers.