duanfen2349
2018-10-11 10:58
浏览 48
已采纳

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

Data is not inserting into table (codeigniter), all the field names are correct as in the view and database table.

This is my model

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

This is my controller

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条回答 默认 最新

  • dongwen1935 2018-10-12 15:26
    已采纳

    Firstly, you have some syntax errors. Try the below. Make note of the change to package_id in the key in data.

        $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..";
    }
    
    已采纳该答案
    打赏 评论
  • duanhuayong6687 2018-10-11 12:27

    try like this.

    //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;
        }
    
    打赏 评论

相关推荐 更多相似问题