我创建了一个用于查询客户、类别和子类别的联系表单,我是使用复选框创建的,它是 Accordion 的格式 feature 和 subfeature 是复选框,我正在使用插入查询来存储数据库,但我无法插入带有子特征的近似特征。 这里有三个表,例如“iv_mail”、iv_mail_features、“iv_mail_subfeatures”。 代码如下所示
$data=array('name'=>$this->input->post('sendname'),
'lastname'=>$this->input->post('sendlname'),
'mobile'=>$this->input->post('sendphone'),
'location'=>$this->input->post('sendlocation'));
$this->db->insert('iv_mail',$data);
$insert_id=$this->db->insert_id();
foreach($this->input->post('features') as $feature){
$data=array('mail_id'=>$insert_id,
'featurename'=> $feature);
$this->db->insert('iv_mail_features',$data);
$sub_id=$this->db->insert_id();
foreach($this->input->post('sub_feature') as $subfeature){
$data=array('feature_id'=>$sub_id,
'subfeaturename'=> $subfeature);
$this->db->insert('iv_mail_subfeatures',$data);
}
}
这里我用的是insert_id,谁能帮帮我
最佳答案
您必须像这样将所有复选框命名为数组。
<input type="checkbox" name="checkbox[]" value="add"><hr>
<input type="checkbox" name="checkbox[]" value="edit"><hr>
<input type="checkbox" name="checkbox[]" value="update"><hr>
<input type="checkbox" name="checkbox[]" value="delete">
那么在你的函数中必须是这样的
$inputnum = count($this->input->post('checkbox'));
for ($i=0;$i < $inputnum; $i++) {
echo $this->input->post('checkbox')[$i].'<hr>';
}
您可以根据需要更改代码。
此外,您可以像这样在一列中将所有功能添加为 json
$inputnum = count($this->input->post('checkbox'));
for ($i=0;$i < $inputnum; $i++) {
$checkbox[] = $this->input->post('checkbox')[$i].'<hr>';
}
json_encode($checkbox);
关于php - 如何使用 codeigniter 将多个复选框值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53189492/