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

wbgh16ku  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(382)

数据没有插入到表中(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();
    }
}
hivapdat

hivapdat1#

首先,你有一些语法错误。试试下面的。请记下密钥输入数据中对包标识的更改。

$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..";
}
lo8azlld

lo8azlld2#

像这样试试。

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

相关问题