dongpo5239
dongpo5239
2014-12-18 05:51

在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 dongyi1748 7年前
                $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'));
                }
    
    点赞 评论 复制链接分享

相关推荐