我在使用代码点火器发布动态文本框数据时遇到问题。我将使用insert_batch()方法执行此操作。
视图
<input name="qty[]" type="text" id="qty[]" size="5" maxlength="5" />
<input type="hidden" name="id[]" id="id[]" value="<?php echo $v_items->product_id ; ?>" />
在控制器中我得到这样的数组结果;
public function submit_in()
{
$data = array(
'id' => $this->input->post('qty'),
'qty' => $this->input->post('id')
);
$data['title'] = 'Damage Product';
$data['subview'] = $this->load->view('admin/cwh/stock_in', $data, true);
$this->load->view('admin/_layout_main', $data);
}
array (size=3)
'id' =>
array (size=13)
0 => string 'qw' (length=2)
1 => string 'qw' (length=2)
2 => string '' (length=0)
3 => string 'qwqw' (length=4)
4 => string '' (length=0)
5 => string '' (length=0)
6 => string 'q' (length=1)
7 => string '' (length=0)
8 => string 'wwq' (length=3)
9 => string '' (length=0)
10 => string '' (length=0)
11 => string '' (length=0)
12 => string 'qw' (length=2)
'qty' =>
array (size=13)
0 => string '74' (length=2)
1 => string '75' (length=2)
2 => string '76' (length=2)
3 => string '77' (length=2)
4 => string '78' (length=2)
5 => string '79' (length=2)
6 => string '80' (length=2)
7 => string '81' (length=2)
8 => string '82' (length=2)
9 => string '83' (length=2)
10 => string '84' (length=2)
11 => string '85' (length=2)
12 => string '86' (length=2)
'title' => string 'Damage Product' (length=14)
所以我不能使用这个数组结果执行batch_insert()方法。 (我认为我的数组结果格式错误了)
我的表结构是
product_data(**pid**,id,qty);
答案 0 :(得分:2)
使用前操纵数据
$this->db->insert_batch('my_table',$data);
这是我的建议:
$arr_qty = $this->input->post('qty');
$arr_id = $this->input->post('id');
$i=-1;
foreach($arr_qty as $qty):
$i++;
$data[] = array(
'qty' => $qty,
'id' => $arr_id[$i]
);
endforeach;
// Then
$this->db->insert_batch('my_table',$data);
告诉我它是否有效。
答案 1 :(得分:0)
批量插入必须循环才能插入批量数据
for($i = 0; $i < sizeof($_POST['name']);$i++)
{
$name = $_POST['name'];
$data = array(
$name[$i];
);
// insert the query here for insert
}
答案 2 :(得分:0)
实际上您分配了错误的数据
'id' => $this->input->post('qty'),//Wrong 'qty'
'qty' => $this->input->post('id') //wrong 'id'
ID ==QTY
和QTY == ID
您的代码错误,应该是
'id' => $this->input->post('id'), //correct
'qty' => $this->input->post('qty') //correct