数据未插入到表中(代码点火器(,所有字段名称都正确,就像在视图和数据库表中一样。
这是我的模型
public function Insert() {
$data = array(
'Package_id' => $this->input->get_post('package_id'),
'cab_id' => $this->input->get_post('cab_id'),
'customer_name' => $this->input->get_post('customer_name'),
'customer_contact'=> $this->input->get_post('customer_contact'),
'customer_email' => $this->input->get_post('customer_email'),
'gender' => $this->input->get_post('gender'),
'no_of_seats' => $this->input->get_post('no_of_seats'));
$this->db->insert('booking',$data);
return ($this->db->affected_rows() != 1) ? false : true;
}
这是我的控制器
public function Insert() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
$this->form_validation->set_rules('customer_name','Name','required');
$this->form_validation->set_rules('no_of_seats','Number Of Seats','required');
$this->form_validation->set_rules('customer_contact','Contact Number','required|exact_length[10]');
$this->form_validation->set_rules('gender','Gender','required');
$this->form_validation->set_rules('customer_email','Email ID','required');
$this->form_validation->set_rules('booking_date','Date','required');
$this->form_validation->set_rules('customer_address','Address','required');
if($this->form_validation->run()) {
$this->load->model('BookingModel');
if($this->BookingModel->Insert()) {
die('<div class="alert alert-success">Successfully Booked!</div><script>$("#response")[0].reset()</script>');
} else {
die('<div class="alert alert-danger">Failed to Book try again!</div>');
}
} else {
echo validation_errors();
}
}
首先,你有一些语法错误。请尝试以下操作。记下对数据键中package_id的更改。
$data = array(
'package_id' => $this->input->post('package_id'), // Lowercase the package id!!
'cab_id' => $this->input->post('cab_id'),
'customer_name' => $this->input->post('customer_name'),
'customer_contact'=> $this->input->post('customer_contact'),
'customer_email' => $this->input->post('customer_email'),
'gender' => $this->input->post('gender'),
'no_of_seats' => $this->input->post('no_of_seats')
);
$res = $this->db->insert('booking',$data);
if ($res->affected_rows() == 1){
echo "Wohoo!";
} else {
echo "Back to StackOverflow..";
}
尝试这样做。
//controller
if($this->form_validation->run()) {
$this->load->model('BookingModel');
$data = array(
'Package_id' => $this->input->get_post('package_id'),
'cab_id' => $this->input->get_post('cab_id'),
'customer_name' => $this->input->get_post('customer_name'),
'customer_contact'=> $this->input->get_post('customer_contact'),
'customer_email' => $this->input->get_post('customer_email'),
'gender' => $this->input->get_post('gender'),
'no_of_seats' => $this->input->get_post('no_of_seats'));
if($this->BookingModel->Insert($data)) {
die('<div class="alert alert-success">Successfully Booked!</div><script>$("#response")[0].reset()</script>');
} else {
die('<div class="alert alert-danger">Failed to Book try again!</div>');
}
} else {
echo validation_errors();
}
}
//model
function Insert($data) {
if ($this->db->insert('booking', $data)) {
return $this->db->insert_id();
}
return 0;
}