如何使用Codeigniter将数组(多行)插入Mysql?

时间:2015-08-04 10:36:13

标签: mysql codeigniter

我有很多搜索,但我发布过somany时间,但我没有得到任何正确答案。

这是我的观看页面 - View

这里我已经针对特定 ref_no 动态添加字段编写查询,请帮助我模型控制器

<div id="login_form">
        <?php echo form_open(base_url().'sample/invoice'); ?>
        <label for="type" class="control-label">Type</label>
        <div><?php echo form_input(array('id'=>'type','name'=>'type'));?></div>

        <label for="ref" class="control-label">REF</label>
        <div><?php echo form_input(array('id'=>'ref','name'=>'ref'));?></div>

        <label for="title" class="control-label">TITLE</label>
        <div><?php echo form_input(array('id'=>'title','name'=>'title'));?></div>

        <div  id="description"><p id="add_field">ADD DESCRIPTION</p></div>

        <label for="doc" class="control-label">Support Doc</label>
        <div><?php echo form_input(array('id'=>'doc','name'=>'attach','type'=>"file"));?></div>

        <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Save" />
    </div>
<script>
            var count = 0;
    $(document).ready(function() {
        $('p#add_field').click(function(){
           count += 1;
            var html='<strong>Description  '+ count +'</strong>'+'<input id="description'+ count +'"name="description[]'+'" type="text" />'+'<input id="description'+ count +'"name="voucher_no[]'+'" type="text" />'+'<input id="description'+ count +'"name="price[]'+'" type="text" /><br />';
            $('#description').append(html);

    });
    });

        </script>

这是我的控制器:

$data1 = array(
    'invoice_type' => $this->input->post('type'),
    'reference_no' => $this->input->post('ref'),
    'des_title' => $this->input->post('title'),


     );

    $data2 = array(
        'reference_no' => $this->input->post('ref'),
        'description' => $this->input->post('des'),
        ); 
 $this->sample_model->insert_entry($data1, $data2);

This My MOdel:

function insert_entry($data1, $data2) {

    $this->db->insert('myinvoice', $data1);
    $this->db->insert('invoice_description', $data2);
}

我想要的是插入一个参考号的多个描述。 myinvoice是父表,invoice_description是子表,当我插入单个数据时,它的工作完美,但我想插入多个描述

4 个答案:

答案 0 :(得分:1)

首先,我们不能在同一页面中多次使用相同的ID。 并在控制器

function abc(){
    $description = $this->input->post("description");
    $voucher_no = $this->input->post("voucher_no");
    $price = $this->input->post("price");
    $i = 0;
    foreach($description as $row){
        $data['description'] = $description[$i];
        $data['voucher_no'] = $voucher_no[$i];
        $data['price'] = $price[$i];
        $this->db->insert("your_table",$data);
        $i++;
    }
}

我希望上面的代码可以帮到你。您可以直接插入控制器,也可以调用模型函数进行插入。

答案 1 :(得分:0)

试试这个 你的控制器将是

$data1 = array(
                  'invoice_type' => $this->input->post('type'),
                  'reference_no' => $this->input->post('ref'),
                  'des_title' => $this->input->post('title'),
                  );
    /*
        Considering description ,voucher_no,price compulsary if user add discription
    */  
    $description = $this->input->post("description");
    $voucher_no = $this->input->post("voucher_no");
    $price = $this->input->post("price");
    $data2 ='';
    if(isset($description) && !empty($description)){
        $i = 0;
        foreach($description as $row){
            $data2[] =array(
                'description'=>$description[$i],
                'voucher_no' => $voucher_no[$i],
                'price'=>$price[$i],        
            );
            $i++;
        }
    }

    $this->sample_model->insert_entry($data1, $data2);

您的模型将是这样的

function insert_entry($data1, $data2) {
        $this->db->insert('myinvoice',$data1);
        if($data2){
            $this->db->insert_batch('invoice_description',$data2);
        }
    }

答案 2 :(得分:0)

希望我的问题和答案对他人有帮助:) 我的观点页面:

<div id="login_form">
            <?php echo form_open(base_url().'sample/invoice'); ?>
            <label for="type" class="control-label">Type</label>
            <div><?php echo form_input(array('id'=>'type','name'=>'type'));?></div>

            <label for="ref" class="control-label">REF</label>
            <div><?php echo form_input(array('id'=>'ref','name'=>'ref'));?></div>

            <label for="title" class="control-label">TITLE</label>
            <div><?php echo form_input(array('id'=>'title','name'=>'title'));?></div>

            <div  id="description"><p id="add_field">ADD DESCRIPTION</p></div>

            <div  id="doc"><p id="add">ADD Support Doc</p></div>

            <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Save" />
        </div>

        <script>
         var count = 0;
        $(document).ready(function() {
        $('p#add_field').click(function(){
           count += 1;
           var html='<strong>Description  '+ count +'</strong>'+'<input id="description'+ count +'"name="description[]'+'" type="text" />'+'<input id="description'+ count +'"name="voucher_no[]'+'" type="text" />'+'<input id="description'+ count +'"name="price[]'+'" type="text" /><br />';
           $('#description').append(html); });
           });
        </script>
        <script>
            var coun = 0;
    $(document).ready(function() {
        $('p#add').click(function(){
           coun += 1;
            var div='<strong>Document  '+ coun +'</strong>'+'<input id="doc'+ coun +'"name="doc[]'+'" type="file" /><br />';
            $('#doc').append(div);

    });
    });

        </script>

这是我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sample extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('sample_model');
    }

    public function index()
    {
        $this->load->view('sample_view');
    }

    function invoice()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('type', 'INVOICE TYPE', 'trim');
        $this->form_validation->set_rules('ref', 'REFERENCE NO', 'trim|required|is_unique[myinvoice.reference_no]');
        $this->form_validation->set_rules('title', 'DESCRIPTION TITLE', 'trim|required'); 
        $this->form_validation->set_rules('description[]', 'DESCRIPTIONS', 'trim'); 
        $this->form_validation->set_rules('voucher_no[]', 'VOUCHER', 'trim'); 
        $this->form_validation->set_rules('price[]', 'PRICE', 'trim'); 
        $this->form_validation->set_rules('doc[]', 'DOCUMENT', 'trim');


        if($this->form_validation->run())
        {
            $this->load->database();
            $this->load->model('sample_model');


            $data1 = array(
                  'invoice_type' => $this->input->post('type'),
                  'reference_no' => $this->input->post('ref'),
                  'des_title' => $this->input->post('title'),
                  );
                  $this->db->insert("myinvoice",$data1);

            $ref =  $this->input->post('ref');
            $description = $this->input->post('description');
            $voucher_no = $this->input->post('voucher_no');
            $price = $this->input->post('price');
             $i = 0;
             if($description){
            foreach($description as $row)
            {
            $data2['ref_no'] = $ref;
            $data2['descriptions'] = $description[$i];
            $data2['voucher'] = $voucher_no[$i];
            $data2['value'] = $price[$i];
              $this->db->insert("invoice_description",$data2);
            $i++;

             }}
                $doc = $this->input->post('doc');
                $ref =  $this->input->post('ref');
            $i = 0;
             if($description){
            foreach($doc as $row)
            {
                $data3['ref_no'] = $ref;
                $data3['attached'] = $doc[$i];
                 $this->db->insert("invoice_attached",$data3);
                $i++;
             }}             
            }else{

            $this->load->view('sample_view');
        }
    }

答案 3 :(得分:0)

您可以尝试

$data = array(
      array( 
          'title'       => $this->input->post('title1'), 
          'category'    => $this->input->post('details1'), 
          'description' => $this->input->post('address1')
      ),
      array( 
          'title'       => $this->input->post('title2'), 
          'category'    => $this->input->post('details12'), 
          'description' => $this->input->post('address2')
      )
);
$this->db->insert('tbl_name', $data);