插入批处理数据数组? [英] insert batch data array?
问题描述
我想在数据库表(mysql)中插入以下数据 insert_batch
:
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);
我想插入他们为:
如何更改php代码和html代码?我做什么?
How should change php code and html code? what do i do?
推荐答案
我假设你正在使用CodeIgniter和要插入的数据库表的名称被称为'units',并且其'id'列是自动增量。
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.
我基于我的解决方案从CodeIgniter用户指南2.0.3版使用调用帮助($ this-> db-> insert_string())
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.
foreach ($data as $row)
{
$error_code = $this->db->insert_string('units', $row);
}
请参阅 http://codeigniter.com/user_guide/database/helpers.html
insert_string
函数,Database类提供似乎采用关联数组,从内部的元素构建 INSERT
语句,执行它然后返回一个数字错误代码。
The insert_string
function that the Database class provides appears to take an associative array, build an INSERT
statement from the elements inside, execute it and then return a numerical error code.
这篇关于插入批处理数据数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!