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

标签 php mysql database codeigniter

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

这是我的模型

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

这是我的 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();
    }
}   

最佳答案

首先,您有一些语法错误。尝试下面的方法。记下 data 中键中 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..";
}

关于php - 没有错误,但数据没有插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52758370/

相关文章:

php - 避免在 MySQL/PHP 中重复显示记录

php - HTML 文本区域对齐

MySQL Workbench 相当于 SQL Server

mysql - Grailsquartz 插件在从 mysql 5.5 升级到 mysql 5.6 时中断

database - 为什么要把IP转成整数来存储呢?

php - 基于数据库查询的不同按钮颜色

java - 我如何检查 MySQL 和 Tomcat 是否正在运行?

sql-server - 从 SQL Server(通过 LinkedServer)选择 Sybase 表得到错误 "db.schema.table was reported to have a "DBCOLUMNFLAGS_ISFIXEDLENGTH"of 16

database - Postgres 不占用系统资源

PHP:获取与另一个字符串数组的子字符串匹配的字符串数组的数组值