使用1个字段插入多行codeigniter

8hhllhi2  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(263)

我有查看提问者,我想插入多行(jawaban和soal),见下文
我的观点

<table>
    <tr>
        <td>1</td>
        <td><input type="hidden" value="1" name="no_soal[]">apakah anda sehat</td>
        <td><input type="checkbox" value="-5" name="jawaban[]"></td>
        <td><input type="checkbox" value="-3" name="jawaban[]"></td>
        <td><input type="checkbox" value="-1" name="jawaban[]"></td>
        <td><input type="checkbox" value="0" name="jawaban[]"></td>
        <td><input type="checkbox" value="1" name="jawaban[]"></td>
        <td><input type="checkbox" value="3" name="jawaban[]"></td>
        <td><input type="checkbox" value="5" name="jawaban[]"></td>
    </tr>
    <tr>
        <td>10</td>
        <td><input type="hidden" value="2" name="no_soal[]">apakah anda rajin mandi</td>
        <td><input type="checkbox" value="-5" name="jawaban[]"></td>
        <td><input type="checkbox" value="-3" name="jawaban[]"></td>
        <td><input type="checkbox" value="-1" name="jawaban[]"></td>
        <td><input type="checkbox" value="0" name="jawaban[]"></td>
        <td><input type="checkbox" value="1" name="jawaban[]"></td>
        <td><input type="checkbox" value="3" name="jawaban[]"></td>
        <td><input type="checkbox" value="5" name="jawaban[]"></td>
    </tr>
</table>
<input type="submit" class="submit btn btn-success" value="Submit"/>

我的控制器

public function add_soal4()
{
    $id= $this->session->userdata('id');
    $userid= $this->session->userdata('id');
    $data = array();
    if ($_POST)
    {
        $i = 0;
        $no_soal = $this->input->post("no_soal");
        $jawaban = $this->input->post("jawaban");
        for ($i = 0; $i < count($this->input->post('id')); $i++)
        {
            $data[] = array(
                'id' => $id,
                'userid' => $userid,
                'no_soal' => $no_soal[$i],
                'jawaban' => $jawaban[$i]
            );
        }
        $this->db->insert_batch('data_jawaban', $data);
    }
}

现在我只能插入1条记录,我需要将所有选定字段作为多个值插入数据库

q35jwt9p

q35jwt9p1#

你应该换掉你的衣服 HTML 格式如下
在视图中

<?=form_open();?>
<table>
    <tr>
        <td>1</td>
        <td><input type="hidden" value="1" name="no_soal[1]">apakah anda sehat</td>
        <td><input type="checkbox" value="-5" name="jawaban[1][]"></td>
        <td><input type="checkbox" value="-3" name="jawaban[1][]"></td>
        <td><input type="checkbox" value="-1" name="jawaban[1][]"></td>
        <td><input type="checkbox" value="0" name="jawaban[1][]"></td>
        <td><input type="checkbox" value="1" name="jawaban[1][]"></td>
        <td><input type="checkbox" value="3" name="jawaban[1][]"></td>
        <td><input type="checkbox" value="5" name="jawaban[1][]"></td>
    </tr><tr>
        <td>10</td>
        <td><input type="hidden" value="2" name="no_soal[2]">apakah anda rajin mandi</td>
        <td><input type="checkbox" value="-5" name="jawaban[2][]"></td>
        <td><input type="checkbox" value="-3" name="jawaban[2][]"></td>
        <td><input type="checkbox" value="-1" name="jawaban[2][]"></td>
        <td><input type="checkbox" value="0" name="jawaban[2][]"></td>
        <td><input type="checkbox" value="1" name="jawaban[2][]"></td>
        <td><input type="checkbox" value="3" name="jawaban[2][]"></td>
        <td><input type="checkbox" value="5" name="jawaban[2][]"></td>
    </tr>
</table>
<input type="submit" class="submit btn btn-success" value="Submit"/>
<?=  form_close();?>

按以下代码替换函数
在控制器中

public function add_soal4(){
    $id = $this->session->userdata('id');
    $userid = $this->session->userdata('id');        
    if($this->input->post() && !empty($id) && !empty($userid)){ 
        $no_soal_list = $this->input->post("no_soal");
        $jawaban_list = $this->input->post("jawaban");
        if(array_filter($jawaban_list)){
            $answer_list = [];
            foreach($jawaban_list as $no_soal => $jawaban_row){
                foreach($jawaban_row as $jawaban){
                    $answer_list[] = compact('id','userid','no_soal','jawaban');
                }                    
            }
            if(array_filter($answer_list)){
                $this->db->insert_batch('data_jawaban', $answer_list);
                echo ($this->db->trans_status()) ? 'Updated' : 'Failed';
            }
        }
    }        
}
8i9zcol2

8i9zcol22#

把钥匙放好 insert 内线 for 循环。

for ($i = 0; $i < count($this->input->post('id')); $i++) {
     $data = array(
         'id' => $id,
         'userid' => $userid,
          'no_soal' => $no_soal[$i],
          'jawaban' => $jawaban[$i]);
          $this->db->insert_batch('data_jawaban', $data);
  }
jvlzgdj9

jvlzgdj93#

你要用 add_soal4 对于php提交和ajax调用,我们应该在 add_soal4 功能
在add\u soal4()中的控制器中

public function add_soal4(){
    $id = $this->session->userdata('id');
    $userid = $this->session->userdata('id');        
    if($this->input->post() && !empty($id) && !empty($userid)){         
        $no_soal_list = $this->input->post("no_soal");
        $jawaban_list = $this->input->post("jawaban");
        if($jawaban_list && array_filter($jawaban_list)){
            $answer_list = [];
            foreach($jawaban_list as $no_soal => $jawaban_row){
                foreach($jawaban_row as $jawaban){
                    $answer_list[] = compact('id','userid','no_soal','jawaban');
                }                    
            }
            if(array_filter($answer_list)){
                $this->db->delete('data_jawaban', compact('id','userid'));
                $this->db->insert_batch('data_jawaban', $answer_list);
                echo ($this->db->trans_status()) ? 'Updated' : 'Failed';
            }
        }
    }
    if(!$this->input->is_ajax_request()){
        // if you have any $this->load->view() file, call it here, dont call the view files outside the loop
    }
}

在视图文件中添加此代码

<script type="text/javascript">
$(function(){
    var autoinsert = setInterval(autosave, 10000); // 10 seconds
    $('form').submit(function(e){
        e.preventDefault();
        clearInterval(autoinsert);
        e.target.submit();
    });
});
function autosave(){
    $.ajax({
        type: 'POST',
        url: '<?= base_url('online/add_soal4'); ?>',
        data: $('form').serialize(),
        cache: false,
        dataType: 'html',
        success: function(data){
            if(data){
                console.log($.parseHTML(data)[0].textContent);
            }            
        },
        error: function(error){
            console.log(error);
        }
    });
}
</script>
mrwjdhj3

mrwjdhj34#

在“视图”中我添加了自动插入脚本间隔10秒,但无法运行。这是我的脚本代码

<script type="text/javascript">
    $(document).ready(function(){ autosave(); });
      function autosave(){
        var t = setTimeout("autosave()", 10000);
        var id = $("#id").val();
        var no_soal = $(this).find('input[name=no_soal]').eq(index).val();
        var jawaban = $(this).find('input[name=jawaban]').eq(index).val();
           $.ajax({
              type: "POST",
              url:"<?php echo base_url('online/add_soal4'); ?>",
              dataType : "JSON",
              data : {'id':id, 'no_soal':no_soal,'jawaban':jawaban}, 
              cache: false,
      }
           });  
      } 
  </script>

相关问题