1
votes

Batchorder

id | total | date

1   810029  19/11/15

Purchorder

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

I've been trying this for a week now. I have this two tables, the batchporder and purchord in the batchporder table I need to insert one row and get the primary id to pass to the purchord insert. in the purchord I need to insert multiple rows so I used 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();

}

Error1

A PHP Error was encountered Severity: Warning

Message: array_keys() expects parameter 1 to be array, string given

Filename: database/DB_query_builder.php

Line Number: 1567

Backtrace:

File: C:\xampp\htdocs\admin\application\models\Common_model.php Line: 23 Function: insert_batch

File: C:\xampp\htdocs\admin\application\controllers\admin\Payment.php Line: 93 Function: create_multiple_table

File: C:\xampp\htdocs\admin\index.php Line: 315 Function: require_once

Error2

Error Number: 21S01/1136

Column count doesn't match value count at row 3

INSERT INTO purchord (amount, code, desc, itemnum, qty, rev, uprice) VALUES ('752004','D2252','cover','4','324','a','2321'), ('58025','D522S','toolbox','2','25','s','2321'), ()

Filename: C:/xampp/htdocs/admin/system/database/DB_driver.php

Line Number: 691

1

1 Answers

1
votes

This is because you have passed $purchord as a multiple array and set $purchord['batchid'] = $batchid; as a single array value in your Model, the batch id should be set as following :

$this->db->insert('batchporder',$batchporder);  
$batchid = $this->db->insert_id();

$purchord = array_map(function($arr) use($batchid){
    return $arr + ['batchid' => $batchid];
}, $purchord);