codeigniter:输入多个插入到数据库

codeigniter : Input multiple insert to database

我用不同的数量做了多次插入。进行交易时,我将其临时容纳或附加 table。但是,仅存储最后数量

this HTML

型号

public function simpan_data($data_pickup){
  $this->simpan_pickup($data_pickup);
  $last_key = $this->db->insert_id();
  $tmp = $this->get_temporary();
  foreach($tmp as $t){
  $data_detail = array(
  'id_pickup'       => $last_key,
  'id_barang'       => $t->id_barang,
  'qty_pickup'      => $this->input->post('qty_pickup')
  );
  $this->db->insert('pickup_detail', $data_detail);
}
} 

public function simpan_pickup($data){
$res = $this->db->insert('pickup', $data);
return $res;
}

控制器

public function add_tem_pickup(){
    $idnya                 = $this->input->post('id_barang');
    $cektmp                = $this->M_pickup->cek_temporary($idnya);
    if ($cektmp > 0) {
        $arr=array(
        'sukses'  => false,
        'pesan'   => 'Barang sudah Pickup'
    );
    //alert
    }else{
    $data_insert      = array(
      'jenis'         => 'PICK',
      'id_barang'     => $idnya
    );
    $this->db->insert('tem_pickup', $data_insert);
    $arr = array(
      'sukses' => true,
      'pesan'   => 'berhasil'
    );
    }
      echo json_encode($arr);
    }

public function simpan_pickup(){
    $data = array(
    'kd_pickup'     => $this->input->post('kd_pickup'),
    'id_cs'         => $this->input->post('id_cs')
    );
    $this->M_pickup->simpan_data($data);
    $this->db->delete('tem_pickup', array('jenis' => 'PICK'));
    echo $this->session->set_flashdata('message','success');
    redirect('backend/pickup');
}

怎么会这样?请帮助

您可以在 ajax 调用成功后设置一个隐藏的 qty_pickup 输入(使用 jquery after()),这里我将这个新输入重命名为 qty_pickup_new[] 将其与现有的 qty_pickup 输入区分开来:

// another js codes here...
//
$(".btnadd").click(function(){ //input append 
        var id_barang                         = $("input[name='id_barang']").val();
        var nama_barang                       = $("input[name='nama_barang']").val();
        var qty_pickup                        = $("input[name='qty_pickup']").val();
        var sisa                              = $("input[name='qty_pickup']").val();
        var harga_jual                        = $("input[name='harga_jual']").val();
        var satuan                            = $("input[name='satuan']").val();
        var kd_pickup                         = $("input[name='kd_pickup']").val();
        // var tgl_pickup                        = $("input[name='tgl_pickup']").val();
        var tipe_dimensi                      = $("input[name='tipe_dimensi']").val();
        var tgl_pickup                        = $("input[name='tgl_pickup']").val();

        $.ajax({
            url: '<?php echo base_url();?>backend/pickup/add_tem_pickup',
            type: 'POST',
            dataType: 'JSON',
            data: {
                id_barang: id_barang,
                qty_pickup: qty_pickup,
                sisa: qty_pickup,
                harga_jual: harga_jual, 
                kd_pickup: kd_pickup,  
                // tgl_pickup: tgl_pickup, 
                satuan: satuan, 
                tipe_dimensi: tipe_dimensi,
                tgl_pickup: tgl_pickup, 
                },
            error: function() {
            },
            success: function(data) {
            if(data.sukses==false){
            alert(data.pesan);
            }else{
            $('#tbody').append("<tr><td>"+kd_pickup+"</td><td>"+nama_barang+"</td><td>"+qty_pickup+"</td><td>"+harga_jual+"</td><td>"+satuan+"</td><td>"+tipe_dimensi+"</td></tr>"); 
            //  added below codes, set array input with id_barang as the key
            $('[name="harga_jual"]').after('<input type="hidden" name="qty_pickup_new[' + id_barang + ']" value="' + qty_pickup + '">');
            }
        }
    });
});

然后您可以通过使用 id_barang 作为键在模型上使用 qty_pickup_new[](就像以前的 javascript 代码一样):

public function simpan_data($data_pickup){
    $this->simpan_pickup($data_pickup);
    $last_key = $this->db->insert_id();
    $tmp = $this->get_temporary();
    foreach($tmp as $t){
        $qty_pickup = $this->input->post('qty_pickup_new')[$t->id_barang]; // using id_barang as qty_pickup_new[] key
        $data_detail = array(
            'id_pickup'       => $last_key,
            'id_barang'       => $t->id_barang,
            'qty_pickup'      => !empty($qty_pickup) ? $qty_pickup : '' // sets data as empty if input is empty
        );
        $this->db->insert('pickup_detail', $data_detail);
    }
}