#CodeIgniter将多个复选框中的每个“已选中”复选框值发送到数据库?

时间:2013-08-07 19:25:36

标签: php mysql forms codeigniter checkbox

我已按以下方式填充数据库中的复选框...

查看:

<div class="control-group warning">  
<label for="room_number" class="control-label">Room Number: </label>
    <div class="controls">
    <?php foreach ($query->result_array() as $row): { ?>
    <input type="checkbox" name="room_number" id="room_number" value="<?php echo $row['room_number'];?>" style="margin:10px" /><?php echo $row['room_number'];?><br>
    <?php } ?>
    <?php endforeach; ?>
    <?php echo form_error('room_number'); ?>
    </div>    
</div>

* 现在当用户提交表单时,我需要填充数据库表,每行都有一个“选中”复选框。但是,下面的代码/方法只提交一行。 *

控制器:

foreach($this->input->post('room_number') as $rm){ // 118
        $newReservation = array (
            'guest_id' => $guest_id,
            'room_number' => $this->input->post('room_number'),
            'room_type' => $this->input->post('room_type'),
            'meal_type' => $this->input->post('meal_type'),
            'extra_beds' => $this->input->post('ext_beds'),
            'purchases' => 0,
            'guest_count' => $this->input->post('number_of_guests'),
            'checkin' => $this->input->post('start_date'),
            'checkout' => $this->input->post('end_date'),
            'duration' => $this->input->post('reservation_duration'),
            'total' => $this->input->post('total'),
            'guest_status' => $this->input->post('guest_status'),
            'payment_status' => 'Pending',
            'travel_agent' => $token,
            'time_stamp' => $now
        );
        $this->group_reservations_model->populate_new_reservations_table($newReservation);

型号:

    function populate_new_reservations_table($newReservation) {
    $this->db->trans_begin();
    $this->db->insert_batch('reservations', $newReservation);

    if ($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        return false;
    }
    else {
        $this->db->trans_commit();
        return true;
    }
}

会返回以下错误:

遇到PHP错误

严重性:警告

消息:为foreach()提供的参数无效

文件名:controllers / group_reservations.php

行号:118

1 个答案:

答案 0 :(得分:0)

您有多个具有相同名称的复选框。使用name="room_number[]"代替name="room_number"

祝你好运;)