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