【问题标题】:how to insert multiple checkbox values into database using codeigniter如何使用codeigniter将多个复选框值插入数据库
【发布时间】:2018-11-07 12:28:06
【问题描述】:

我创建了一个用于查询客户、类别和子类别的联系表,我是使用复选框创建的,它是手风琴的格式 特征和子特征是复选框,我使用插入查询来存储数据库,但我不能插入带有子特征的近似特征。 这里有三个表,例如'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,任何人都可以帮助我

【问题讨论】:

  • 你能简单解释一下吗?现在你遇到了什么错误?

标签: php mysql codeigniter


【解决方案1】:

您必须将所有复选框命名为这样的数组。

<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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多