没有错误,但是没有插入数据

时间:2018-10-11 10:58:37

标签: php mysql database codeigniter

数据未插入表(codeigniter)中,所有字段名称均与视图和数据库表中的正确。

这是我的模特

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

2 个答案:

答案 0 :(得分:0)

尝试这样。

//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;
    }

答案 1 :(得分:0)

首先,您有一些语法错误。请尝试以下。记录数据中key_id对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..";
}