dongpo5239 2014-12-18 05:51
浏览 64
已采纳

在codeigniter中插入多个复选框值

I want to make multiple insert checkbox in same ID. I have table like this:

id  id_fleet unit_id
1    1       CAO27
2    1       CA098
3    2       CA078

in one form submit I select in id_fleet 1 and I check 2 unit_id. I try with my code it only save one check.

this is my code in controller:

$fleet = $this->input->post('id_fleet');
            $unit_id = $this->input->post('unit_id');

            $records = array();

            for ($i=0; $i < count($unit_id) ; $i++) { 
                $data = array(
                    'id_fleet' => $fleet,
                    'unit_id' => $unit_id
                );

                array_push($records, $data);
            }

            $query = $this->database_three->query("select
                count(id_fleet_member) as unit from fleet_member 
                where id_fleet = '$fleet'");
            $ans = $query->row();

            if ($ans->unit > 0)
            {
                $this->session->set_flashdata('message', generateErrorMessage('Data gagal ditambah'));
                redirect(site_url('fleet_member'));     
            }
            else
            {
                for ($i=0; $i <sizeof($data['unit_id']) ; $i++) { 
                    $query = "insert into fleet_member (id_fleet, unit_id) values ('".$data['id_fleet']."','".$data['unit_id'][$i]."')";
                    $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
                    redirect(site_url('fleet_member'));
                }

            }

this is my model:

function add_fleet_member($data)
    {
        $this->database_three->insert($this->tbl_fleet_member, $data);
        if ($this->database_three->affected_rows() > 1)
        {
            return true;
        }else{
            return false;
        }
    }

and this is in view:

<div class="checkbox">
       <?php foreach ($unit_list as $data) :?>
       <label>
          <input type="checkbox" name="unit_id[]" value="<?php echo $data->unit_id ?>"><?php echo $data->unit_id ?>
       </label>
        <?php endforeach?>
</div>

can you help me why my code only save one value checkbox?

thank you

  • 写回答

1条回答 默认 最新

  • dongyi1748 2014-12-18 06:52
    关注
                $fleet = $this->input->post('id_fleet');
                $unit_id = $this->input->post('unit_id');
    
                $records = array();
    
                for ($i=0; $i < count($unit_id) ; $i++) { 
                    $records[] = array('id_fleet' => $fleet,'unit_id' => $unit_id[$i]);
                }
    
                $query = $this->database_three->query("select
                    count(id_fleet_member) as unit from fleet_member 
                    where id_fleet = '$fleet'");
                $ans = $query->row();
    
                if ($ans->unit > 0)
                {
                    $this->session->set_flashdata('message', generateErrorMessage('Data gagal ditambah'));
                    redirect(site_url('fleet_member'));     
                }
                else
                {
    
                    foreach ($records as $key => $value) {
                        $query = "insert into fleet_member (id_fleet, unit_id) values ('".$value['id_fleet']."','".$value['unit_id']."')"; 
                        $this->db->query($query);
                    }
                    $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
                    redirect(site_url('fleet_member'));
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化