批量订购
id | total | date
1 810029 19/11/15 采购订单
id | itemnum | code | rev | desc | qty | uprice | amount | batchid 1 4 D2252 A Cover 324 2321 752004 1 2 2 D522S S Toolbox 25 2321 58025 1 我已经尝试了一个星期了。我有这两个表,即batchporder表中的batchporder和purchord ,我需要插入一行并获取主ID才能传递给purchord插入。在purchord中,我需要插入多行,所以我使用了insert_batch。
控制者
public function post_multiple_table(){
$this->load->model('Common_model', 'com_model', TRUE);
if ($_POST) {
$batchporder_input_data = array();
$batchporder_input_data['total'] = $this->input->post('date');
$batchporder_input_data['total'] = $this->input->post('total');
$batchporder_input_data['ref'] = $this->input->post('ref');
$batchporder_input_data['freight'] = $this->input->post('freight');
$batchporder_input_data['pload'] = $this->input->post('pload');
$batchporder_input_data['pdest'] = $this->input->post('pdest');
$batchporder_input_data['ddate'] = $this->input->post('ddate');
$batchporder_input_data['term'] = $this->input->post('term');
$itemnum = $this->input->post('itemnum');
$code = $this->input->post('code');
$rev = $this->input->post('rev');
$desc = $this->input->post('desc');
$qty = $this->input->post('qty');
$uprice = $this->input->post('uprice');
$amount = $this->input->post('amount');
for ($i=0; $i < sizeof($itemnum); $i++){
$purchord_input_data[$i] = array('itemnum' => $itemnum[$i],
'code' => $code[$i],
'rev' => $rev[$i],
'desc' => $desc[$i],
'qty' => $qty[$i],
'uprice' => $uprice[$i],
'amount' => $amount[$i]
);
}
// echo '<pre>';
// var_dump($purchord_input_data);
// var_dump($batchporder_input_data);
// echo '</pre>';
$checking_insert = $this->com_model->create_multiple_table($batchporder_input_data, $purchord_input_data);
if($checking_insert){
redirect(base_url('admin/payment/all_payments'));
}
else{
redirect(base_url('admin/dashboard'));
}
}
} 模型
//-- order function
public function create_multiple_table($batchporder,$purchord){ $this->db->insert('batchporder',$batchporder);
$batchid = $this->db->insert_id();
$purchord['batchid'] = $batchid;
$this->db->insert_batch('purchord',$purchord);
return $insert_id = $this->db->insert_id();
} 错误1
遇到PHP错误严重性:警告
消息:array_keys()期望参数1为数组,给定字符串
文件名:database / DB_query_builder.php
行号:1567
回溯:
文件:C:\ xampp \ htdocs \ admin \ application \ models \ Common_model.php行:23功能:insert_batch
文件:C:\ xampp \ htdocs \ admin \ application \ controllers \ admin \ Payment.php行:93函数:create_multiple_table
文件:C:\ xampp \ htdocs \ admin \ index.php行:315功能:require_once
错误2
错误编号:21S01 / 1136
列数与第3行的值数不匹配
INSERT INTO purchord(amount,code,desc,itemnum,qty,rev,uprice)VALUES( '752004', 'D2252', '盖', '4', '324', '一个', '2321'),( '58025', 'D522S', '工具箱','2','25','s','2321'),()
文件名:C:/xampp/htdocs/admin/system/database/DB_driver.php
行号:691
问题来源于stack overflow
这是因为您已将其$purchord作为多个数组传递并$purchord['batchid'] = $batchid;在您的中设置为单个数组值Model,所以批处理ID的设置应如下所示:
$this->db->insert('batchporder',$batchporder);
$batchid = $this->db->insert_id();
$purchord = array_map(function($arr) use($batchid){ return $arr + ['batchid' => $batchid]; }, $purchord);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。