【问题标题】:How to Insert Arrays (Multiple rows) into Mysql using Codeigniter?如何使用 Codeigniter 将数组(多行)插入 Mysql?
【发布时间】:2015-08-04 10:36:13
【问题描述】:

我搜索了很多,我已经发布了很多次,但我没有得到任何正确的答案。

这是我的查看页面 - View

这里我为特定 ref_no 编写了一个动态添加字段的查询,所以请帮助我了解 model 和 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>

这是我的控制器:

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

这是我的模特:

function insert_entry($data1, $data2) {

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

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

【问题讨论】:

  • 你想要模型和控制器代码?
  • 我想将数据插入mysql数据库
  • 发布一些您尝试过的模型和控制器代码。
  • @user1048123 请我编辑我的问题请帮助我
  • 您从哪里获得 $data1 和 $data2 值。它不是你的形式

标签: mysql codeigniter


【解决方案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++;
    }
}

希望以上代码对您有所帮助。您可以直接插入到控制器中,也可以调用模型函数进行插入。

【讨论】:

  • 它不会工作 :( 没有错误,但是当我提交时,我无法在我的数据库中获取任何数据
  • 兄弟为此我混合了你的 3 个代码我会更新我的答案 ok va :)
【解决方案2】:

试试这个 您的控制器将是

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

【讨论】:

  • 没有错误,但数据不会插入我的数据库,只是在我提交时清理文件
  • 您能否在 insert_entry 之前在控制器中打印 $data2 和 $data1 值并检查您得到的值是什么
  • 相应地更新您的问题。以便我们提供帮助
【解决方案3】:

希望我的问题和答案对其他人有所帮助:) 我的浏览页面:

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

【讨论】:

    【解决方案4】:

    你可以试试这个

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

    【讨论】:

    • 感谢杰西编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2020-11-20
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多