How to Insert Arrays (Multiple rows) into Mysql us

2019-08-26 17:41发布

问题:

i have search alot and i have post somany times but i didnt get any proper answer.

This is my view page - View

here i have write a query for add fields dynamically for particular ref_no so help me for model and controller

<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>

This is My Controller :

$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);
}

What i want is insert mutiple descriptions for one reference number. myinvoice is parent table and invoice_description is child table when i insert single data its works perfectly but i want insert multiple descriptions

回答1:

first of all we cannot use same id more than once in the same page. And in controller

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++;
    }
}

I hope the above code helps you. You can directly insert in controller or you can call a model function to do that insertion.



回答2:

try this Your controller will be

$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);

Your model will be like this

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


回答3:

Hope my question and answer will help to others :) My View Page :

<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>

This is My Controller :

<?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');
        }
    }