CodeIgniter:当表单数据通过数组插入表时,只有“0”值写入数据库

时间:2013-07-03 05:25:26

标签: php codeigniter post

一切正常,除了在表单上写入数据,它写入零。提交一些值后,表(new_reservation)如下所示:

enter image description here

PK:res_id(AI,NN)

非空:除“ext_bed”之外的所有

型号:(reservations_model.php)

class Reservations_model extends CI_Model {

function add_reservations($data) {
    $this->db->insert('new_reservation', $data);
    return;
}

}

控制器:(reservations.php)

class Reservations extends CI_Controller {

function index() {
    $this->load->view('/main/new_reservation');
    $this->load->model('reservations_model');
}

function add_reservations() {       
    $data = array (
    'room_type' => $this->input->post('room_type'),
    'meal_type' => $this->input->post('meal_type'),
    'bed_type' => $this->input->post('bed_type'),
    'ext_beds' => $this->input->post('ext_beds'),
    'number_of_guests' => $this->input->post('number_of_guests'),
    'start_date' => $this->input->post('start_date'),
    'end_date' => $this->input->post('end_date'),
    'reservation_duration' => $this->input->post('reservation_duration'),
    'room_number' => $this->input->post('room_number'),
    'total' => $this->input->post('total')
    );
$this->reservations_model->add_reservations($data);
$this->index();
}
}

查看:(/ views / main文件夹中的new_reservation.php)

<?php echo form_open('reservations/add_reservations'); ?>

<p>      
<label for="room_type">Room Type*: </label>
<select id="room_type"></select>
</p>

<p>      
<label for="meal_type">Meal Type*: </label>
<select size="1" multiple="multiple" id="meal_type"></select>
</p>

<p>      
<label for="bed_type">Bed Type*: </label>
<select id="bed_type"></select>
</p>

<p>      
<label for="ext_beds">Extra Bed(s): </label>
<input type="text" id="ext_beds" />
</p>

<p>      
<label for="number_of_guests">Number of Guests: </label>
<input type="text" id="number_of_guests" />
</p>

<p>      
<label for="start_date">Check-in Date*: </label>
<input  type="text" id="start_date" />
</p>

<p>      
<label for="end_date">Checkout Date*: </label>
<input type="text" id="end_date" />
</p>

<p>      
<label for="reservation_duration">Duration: </label>
<input type="text" id="reservation_duration" />(Nights)
</p>

<p>      
<label for="room_number">Room Number: </label>
<input type="text" id="room_number" />
</p>

<p>      
<label for="total">Total: </label>
<input type="text" disabled="disabled" id="total" readonly="readonly" />
</p>   

<p>
<input class="btn" type="reset" />
<input class="btn btn-primary" type="submit" value="Check-in" />
</p>

<?php echo form_close(); ?>

4 个答案:

答案 0 :(得分:2)

在视图中

使用name而不是id

代码中的示例示例

<p>      
<label for="start_date">Check-in Date*: </label>
<input  type="text" id="start_date" name="start_date" />
</p>

答案 1 :(得分:1)

您的表单数据..他们应该有一个名称值ex:

<input type="text" id="end_date" name="end_data"/>

答案 2 :(得分:1)

将名称与id一起使用,例如:

<input type="text" id="number_of_guests" name="number_of_guests">

用于所有输入语句

答案 3 :(得分:0)

使用名称而不是ID

获取值
<input type="text" id="number_of_guests" name="number_of_guests">