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 SAP HANA SQL Script 。SUM OVER 怎么加where
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败