如何在Codeigniter中将多个数据从动态表插入mysql

时间:2019-04-29 03:27:42

标签: php mysql codeigniter

有人可以帮我吗?
我尝试将多个动态框中的值从Bootstrap保存到MySQL?

我尝试了很多建议,但是只插入了第一行,而没有其他来自添加表的数据。似乎循环无法正常工作。

这是我插入数据的代码之一。

我的观点:

<div class="container">
   <div class="row clearfix">
      <div class="col-md-12 column">
         <table class="table table-bordered table-hover" 
            id="tab_logic">
            <thead>
               <tr >
                  <th class="text-center">
                     Bilangan
                  </th>
                  <th class="text-center">
                     Nama
                  </th>
                  <th class="text-center">
                     Tarikh Lahir
                  </th>
                  <th class="text-center">
                     Tempat Lahir
                  </th>
                  <th class="text-center">
                     Warganegara
                  </th>
               </tr>
            </thead>
            <tbody>
               <tr id='addr0'>
                  <td>
                     1
                  </td>
                  <td>
                     <input type="text" name='nama_siblings[]'  
                        placeholder='Nama' class="form-control"/>
                  </td>
                  <td>
                     <input type="text" name='tarikh_lahir_siblings[]' 
                        placeholder='Tarikh Lahir' class="form-control"/>
                  </td>
                  <td>
                     <input type="text" name='tempat_lahir_siblings[]' 
                        placeholder='Tempat Lahir' class="form-control"/>
                  </td>
                  <td>
                     <input type="text" name='warganegara_siblings[]' 
                        placeholder='Warganegara' class="form-control"/>
                  </td>
               </tr>
               <tr id='addr1'></tr>
            </tbody>
         </table>
      </div>
   </div>
   <tr>
      <td height="100"></td>
   </tr>
   <a id="add_row" class="btn btn-default pull-left">Tambah</a><a id='delete_row' class="pull-right btn btn-default">Buang</a>
</div>
<SCRIPT>
   $(document).ready(function(){
      var i=1;
      $("#add_row").click(function(){
      $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='nama_siblings"+i+"' type='text' placeholder='Nama' class='form-control input-md'  /> </td><td><input  name='tarikh_lahir_siblings"+i+"' type='text' placeholder='Tarikh lahir'  class='form-control input-md'></td><td><input  name='tempat_lahir_siblings"+i+"' type='text' placeholder='Tempat Lahir'  class='form-control input-md'></td><td><input  name='warganegara_siblings"+i+"' type='text' placeholder='Warganegara'  class='form-control input-md'></td>");

      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      i++; 
     });
     $("#delete_row").click(function(){
         if(i>1){
         $("#addr"+(i-1)).html('');
         i--;
         }
     });
});

</script>

我的控制器:

$xsscleanibu_id = $this->security->xss_clean($this->input->post('ibu_id')); 
$xsscleannama_siblings = $this->security->xss_clean($this->input->post('nama_siblings'));   
$xsscleantarikh_lahir_siblings = $this->security->xss_clean($this->input->post('tarikh_lahir_siblings'));   
$xsscleantempat_lahir_siblings = $this->security->xss_clean($this->input->post('tempat_lahir_siblings'));   
$xsscleanwarganegara_siblings = $this->security->xss_clean($this->input->post('warganegara_siblings')); 

foreach($rows as $row){

   $nama_siblings = $nama_siblings[$index];
   $ibu_id = $ibu_id[$index];
   $tarikh_lahir_siblings = $tarikh_lahir_siblings[$index];
   $tempat_lahir_siblings = $tempat_lahir_siblings[$index];
   $warganegara_siblings = $warganegara_siblings[$index];

   $data2 = array(
   'ibu_id'=>$xsscleanibu_id,
   'nama_siblings'=>$row,
   'tarikh_lahir_siblings'=>$xsscleantarikh_lahir_siblings,
   'tempat_lahir_siblings'=>$xsscleantempat_lahir_siblings,
   'warganegara_siblings'=>$xsscleanwarganegara_siblings
   );
   $this->inq_model_dev->insertMonday($data2);
}

   if($row)
   { 
      echo "<script>alert('Data Berjaya disimpan');
window.location = '".base_url('index.php/efast/dashboard_efast')."';</script>";
    }
    else 
    { 
      echo "<script>alert-danger('Data gagal disimpan');window.location = '".base_url('index.php/efast/pendaftaran')."';</script>";
    }

我的模特:

function insertMonday($data2){

     foreach($data as $data2){
        $this->db->insert('data_anak', $data2);
     }
}

1 个答案:

答案 0 :(得分:0)

当您插入多个数据时,请使用 insert_batch 代码初始化器默认功能。

 function insertMonday($data2){

        foreach($data as $data2){
            $this->db->insert_batch('data_anak', $data2);
        }

    }