【问题标题】: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如何插入两个表;一个将插入 1 行,另一个将插入多行,两个表的一列具有相同的值
【发布时间】:2020-03-11 04:56:00
【问题描述】:

批处理顺序

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

我已经尝试了一个星期了。 我有这两张表,batchporderpurchord 在 batchporder 表中,我需要插入一行并获取 主要 id 传递给 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 是数组,给定字符串

文件名:数据库/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 行的值数不匹配

插入purchordamountcodedescitemnumqtyrevuprice)值('752004','D2252'', ,'4','324','a','2321'), ('58025','D522S','工具箱','2','25','s','2321'), ()

文件名:C:/xampp/htdocs/admin/system/database/DB_driver.php

行号:691

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    这是因为您已将$purchord 作为多个数组传递,并在Model 中将$purchord['batchid'] = $batchid; 设置为单个数组值,批处理ID 应设置如下:

    $this->db->insert('batchporder',$batchporder);  
    $batchid = $this->db->insert_id();
    
    $purchord = array_map(function($arr) use($batchid){
        return $arr + ['batchid' => $batchid];
    }, $purchord);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 2017-04-21
      相关资源
      最近更新 更多