dongpo5239 2014-12-17 21: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-17 22: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 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部