简体   繁体   English

我无法将数据插入数据库(codeigniter)

[英]I can not insert data into database (codeigniter)

When i add data into database. 当我将数据添加到数据库中时。 I get message error: 我收到消息错误:

Field 'order' doesn't have a default value
INSERT INTO `pages` (`title`, `slug`, `body`, `parent_id`) VALUES ('About', 'Abot', 'About', 5)

MY_Model: MY_型号:

public function save($data, $id = NULL){

// Insert
    if ($id === NULL) {
    !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
    $this->db->set($data);
    $this->db->insert($this->_table_name);

    $id = $this->db->insert_id();
}
// Update
else {
    $filter = $this->_primary_filter;
    $id = $filter($id);
    $this->db->set($data);
    $this->db->where($this->_primary_key, $id);
    $this->db->update($this->_table_name);
}

return $id;
}

Controller: 控制器:

public function edit($id = NULL) {

    // Fetch a page or set a new one
    if($id) {
        $this->data['page'] = $this->page_m->get($id);
        count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
    } else {
        $this->data['page'] = $this->page_m->get_new();
    }

    // Pages for dropdown
    $this->data['pages_no_parents'] = $this->page_m->get_no_parents();
    //dump($this->data['pages_no_parents']);

    // Set up the form
    $rules = $this->page_m->rules;
    $this->form_validation->set_rules($rules);

    // Process the form
    if($this->form_validation->run() == TRUE) {
        //We can login and redirect
        $data = $this->page_m->array_from_post(array('title', 'slug', 'body', 'parent_id'));
        $this->page_m->save($data, $id);
        redirect('admin/page');
    }
    // Load the view
    $this->data['subview'] = 'admin/page/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

database: 数据库:

 id | title    | slug    | order | body | parent_id
 1  | Homepage |  /      |    1  | abc  |  0
 2  | About    | contact |    0  | abc  |  0

when i run query in phpmyadmin: 当我在phpmyadmin中运行查询时:

INSERT INTO `pages` (`title`, `slug`, `body`, `parent_id`) VALUES ('About', 'Abot', 'About', 5)

it's ok. 没关系。

You need to either insert some value for order 您需要为订单插入一些值

INSERT INTO `pages` (`title`, `slug`, `order`, `body`, `parent_id`) VALUES ('About', 'Abot', NULL, 'About', 5)

or change the table to have a default value 或将表更改为默认值

ALTER TABLE `pages` CHANGE `status` `status` VARCHAR(255) NOT NULL DEFAULT 'whatever default you want';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM