Php 在codeigniter中向Postgres插入多个复选框值
我想在同一ID中创建多个插入复选框。我有如下表:Php 在codeigniter中向Postgres插入多个复选框值,php,sql,postgresql,codeigniter,checkbox,Php,Sql,Postgresql,Codeigniter,Checkbox,我想在同一ID中创建多个插入复选框。我有如下表: id id_fleet unit_id 1 1 CAO27 2 1 CA098 3 2 CA078 在一个表单提交中,我在id\u fleet 1中选择,然后检查2个unit\u id。我尝试使用我的代码,它只保存一个检查 这是我在控制器中的代码: $fleet = $this->input->post('id_fleet'); $unit_id = $
id id_fleet unit_id
1 1 CAO27
2 1 CA098
3 2 CA078
在一个表单提交中,我在id\u fleet 1中选择,然后检查2个unit\u id。我尝试使用我的代码,它只保存一个检查
这是我在控制器中的代码:
$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]."')";
pg_query($query);
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
redirect(site_url('fleet_member'));
}
}
鉴于此:
<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>
$fleet=$this->input->post('id_fleet');
$unit_id=$this->input->post('unit_id');
//所有选项都作为此数组中的记录
$records=array();
对于($i=0;$i$fleet,
“单位id”=>$unit\U id[$i]
);
//将一条记录推送到其他记录
数组推送($records,$data);
}
$this->load->model(“您的模型名称”);
//插入多个记录使用插入批处理活动记录
$this->your_model_name->add_fleet_member($records);
//模型函数编辑
函数add\u fleet\u member($data=array())
{
如果($this->database\u three->insert\u batch($this->tbl\u fleet\u member,$data))
{
返回true;
}
其他的
{
返回false;
}
}
你能在上面检查我的编辑吗?我一直用你的代码编辑,但它只保存一个值复选框对不起,太晚了,是的,我会的
<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>
$fleet = $this->input->post('id_fleet');
$unit_id = $this->input->post('unit_id');
//all options are as records in this array
$records = array();
for($i=0;$i<count($unit_id);$i++)
{
//for one record
$data = array(
'id_fleet' => $fleet,
'unit_id' => $unit_id[$i]
);
//push one record to other records
array_push($records,$data);
}
$this->load->model("your_model_name");
//insert multiple records use insert batch active record
$this->your_model_name->add_fleet_member($records);
//model function edited
function add_fleet_member($data=array())
{
if($this->database_three->insert_batch($this->tbl_fleet_member, $data))
{
return true;
}
else
{
return false;
}
}