[英]how to insert into two tables; one will insert 1 row and the other one will insert multiple rows, the two tables has one column with the same value
批處理
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 插入。 在采購中,我需要插入多行,所以我使用了 insert_batch。
Controller
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'));
}
}
}
Model
//-- 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 是數組,給定字符串
文件名:數據庫/DB_query_builder.php
行號:1567
回溯:
文件:C:\xampp\htdocs\admin\application\models\Common_model.php 行:23 Function:insert_batch
文件:C:\xampp\htdocs\admin\application\controllers\admin\Payment.php 行:93 Function:創建
文件:C:\xampp\htdocs\admin\index.php 行:315 Function:require_once
錯誤2
錯誤號:21S01/1136
列數與第 3 行的值數不匹配
INSERT INTO purchord
( amount
, code
, desc
, itemnum
, qty
, rev
, uprice
) VALUES ('752004','D2252','cover','4','324','a','2321'), ( '58025','D522S','工具箱','2','25','s','2321'),()
文件名:C:/xampp/htdocs/admin/system/database/DB_driver.php
行號:691
這是因為您已將$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.