如何将多个值从chechbox插入到codeigniter中的数据库中

时间:2016-02-07 05:29:32

标签: php mysql codeigniter checkbox multivalue

我尝试使用codeigniter将所选复选框中的值插入数据库,但是当我提交表单时,没有任何事情发生 这是我的观看代码

<?php if (!empty($BW) && is_array($BW)) {?>
    <div class="form-group has-feedback <?php set_validation_style('P_BW_ID')?>">
    <?php echo form_label('Bandwidth', 'P_BW_ID', array('class' => 'control-label')) ?>
    <div class="panel panel-default">
        <div class="panel-body" style="max-height: 200px;overflow-y:scroll;">
        <?php foreach($BW as $row): ?>
        <?php echo form_checkbox('P_BW_ID',  $row->P_BW_ID,'', FALSE, 'id="bandwidth" class="form-control"') ;
        echo '&nbsp;&nbsp;&nbsp;'.$row->BANDWIDTH;
        ?>
        </br></br>
        <?php endforeach ?>
        <?php set_validation_icon('P_BW_ID') ?>
        <?php echo form_error('P_BW_ID', '<span class="help-block">', '</span>');?>
        </div>
     </div>
    </div>

这是我的控制器

public function tambah()
{       
    $id = $this->input->post('P_NW_SERVICE_ID');
    $data['id'] = $id;
    $this->data['serv_name'] = $this->bw_package->getservname();
    $this->data['packname'] = $this->bw_package->getpackname($id);
    $this->data['BW'] = $this->bw_package->getbandwidth();
    $this->data['main_view'] = 'admin/bw_package_form';
    $this->data['form_action'] = site_url('admin/bw_nw_serv_pack/tambah');
    // Data untuk form.
    if (! $_POST) {
       $bw_package = (object) $this->bw_package->default_value;
    } else {
        $bw_package = $this->input->post(null, true);
    }

    // Validasi.
    if (! $this->bw_package->validate('form_rules')) {
        $this->data['values'] = (object) $bw_package;
        $this->load->view($this->layout, $this->data);
        return;
    }
    // Simpan ke DB.
    if ($this->bw_package->tambah($bw_package)) {
        $this->session->set_flashdata('pesan', 'data berhasil disimpan. Kembali ke halaman ' . anchor('admin/bw_nw_serv_pack', 'Bandwidth Package.', 'class="alert-link"'));
        redirect('admin/bw_nw_serv_pack/sukses');
    } else {
        $this->session->set_flashdata('pesan_error', 'data gagal disimpan. Kembali ke halaman ' . anchor('admin/bw_nw_serv_pack', 'Bandwidth Package.', 'class="alert-link"'));
        redirect('admin/bw_nw_serv_pack/error');
    }
}

这是我的模特

public function tambah($bw_pacakage)
{
    $bw_pacakage = (object) $bw_pacakage;
    return $this->insert_batch($bw_pacakage);
}
谁能帮帮我?

之前谢谢

1 个答案:

答案 0 :(得分:1)

foreach复选框数组 例如:

MyClass.child

将数据库转换数组保存到字符串

<input type="checkbox" name="example[]" value="red"/>
<input type="checkbox" name="example[]" value="blue"/>
<input type="checkbox" name="example[]" value="brown"/>