php - 如何使用 codeigniter 将多个复选框值插入数据库

标签 php mysql codeigniter

我创建了一个用于查询客户、类别和子类别的联系表单,我是使用复选框创建的,它是 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/

相关文章:

php - 谁能解释 MariaDB 这种奇怪的行为?

php - 何时在 Zend Framework 中使用模块?

php - 如何从远程服务器上的MySql数据库获取本地字符

MySQL别名和表名

mysql - 在 MySQL 中创建累积和列

codeigniter - 如何使用 CodeIgniter PART 2 测试 Controller ?

php - 波形不如应有的平滑

php - 索引中的 Wordpress 插件样式

php - CodeIgniter,查询 View 中仅显示1条记录

javascript - 发布或获取 ajax 请求中不存在的数据