【问题标题】:How can i insert multiple arrays into mysql database using codeginiter?如何使用codeigniter将多个数组插入mysql数据库?
【发布时间】:2015-07-23 04:43:45
【问题描述】:

我想要的是,我有一个 uniq id 调用 num 我想为那个唯一的 num 插入 多个描述。我有一个表单来动态地添加文件,所以我可以添加我想要的字段。当我尝试插入一行数据时,它的工作完美,当我尝试插入多行数据时,它不起作用。

我的视图页面:

<form name="codexworld_frm" action="" method="post">

<div class="field_wrapper">
<input type="text" name="num" value=""/><br />

<input type="text" name="description[]" value=""/><input type="text" name="voucher_no[]" value=""/><input type="text" name="price[]" value=""/>
        <a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>

</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>

我的控制器:

$data = array(
                'no' => $this->input->post('num'),
                'descriptions' => $this->input->post('description'),
                'voucher' => $this->input->post('voucher_no'),
                'des_price' => $this->input->post('price'),
            );

            $this->multi_model->form_insert($data);

            $data['message'] = 'Data Inserted Successfully';
            //Loading View
            $this->load->view('multi_view', $data);

我的模特:

function form_insert($data){

    $this->db->insert('tbl_description', $data);
    }

如果我在我的模型中使用 foreache 循环,我认为它会起作用,但我该如何使用?

当我使用 print_r() 函数时,这是输出

    1001
Array
    (
        [0] => description1
        [1] => description2
        [2] => description3
    )
    Array
    (
        [0] => voucher 1
        [1] => voucher 2
        [2] => voucher 3
    )
    Array
    (
        [0] => 100
        [1] => 200
        [2] => 300
    )

【问题讨论】:

  • 你可以使用$this-&gt;db-&gt;insert_batch();
  • 这也dsnt工作:(
  • 你想多次插入描述还是整个$data?
  • numid 用于描述,因此每个描述都有 voucher_noprice跨度>

标签: codeigniter


【解决方案1】:

请参阅this 链接,它使用不带循环的多个插入,或者您可以在控制器计数描述帖子中使用您的 foreach 循环并通过它。

    $data = array();

    $count = count($this->input->post['description']);

    for($i=0; $i < $count; $i++) {
        $data[] = array(
            'no'=>$this->input->post('num'),
            'descriptions' => $this->input->post['descriptions'][$i],
            'voucher' => $this->input->post['voucher'][$i],
            'des_price' => $this->input->post['des_price'][$i],
           );
    }
    $this->db->insert_batch('tbl_description', $data);

【讨论】:

  • 在此链接中只有两个数组 ryt 但我想动态添加数组它将 b 1 或 2 或 3 或........等
  • 您可以 print_r($_POST['description']) 并查看在 foreach 循环中用于它的结果数组,其他帖子是相同的,并且您已完成使用 foreach 循环进行多次插入。
【解决方案2】:
you can insert object wise.

in your controller:-

$value=$this->input->post('num');
$count=count($val);
for($i=0; $i<$count; $i++){

$data['no']=$this->input->post('num')[$i];
$data['description']=$this->input->post('description')[$i];
$data['voucher']=$this->input->post('voucher')[$i];
$data['prize']=$this->input->post('prize')[$i];

$this->multi_model->form_insert($data);
}

【讨论】:

    【解决方案3】:

    希望对你有帮助..

    控制器

    //if voucher_no is required..
    
    $voucher_no = $this->input->post('voucher_no');    
    $count = count($voucher_no);
    if ($count > 0) {
       for ($i = 0; $i < $count; $i++) {
           if (!empty($voucher_no[$i])) {
              $data = array(
                'no' => $this->input->post('num'),
                'descriptions' => $this->input->post('description')[$i],
                'voucher' => $this->input->post('voucher_no')[$i],
                'des_price' => $this->input->post('price')[$i],
                );
    
              $this->multi_model->form_insert($data);
            }
          } 
       }
    
     $data['message'] = 'Data Inserted Successfully';
     //Loading View
     $this->load->view('multi_view', $data);
    

    让我们知道结果..

    【讨论】:

    • num 是我的主键,每个描述都有 vocher_no 和 price
    【解决方案4】:

    改变模型如下。

    function form_insert($data){
        foreach($data['description'] as $key=>$des)
        {
          $savedata = array(
                'no' => $data('no'),
                'descriptions' => $des,
                'voucher' => $data['voucher'][$key],
                'des_price' => $data['dec_price'][$key],
            );
          $this->db->insert('tbl_description', $savedata);
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多