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

时间:2014-02-13 16:21:16

标签: php mysql codeigniter

当我将数据添加到数据库中时。我收到消息错误:

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

MY_Model:

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

控制器:

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

数据库:

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

当我在phpmyadmin中运行查询时:

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

没关系。

1 个答案:

答案 0 :(得分:0)

您需要为订单

插入一些值
INSERT INTO `pages` (`title`, `slug`, `order`, `body`, `parent_id`) VALUES ('About', 'Abot', NULL, 'About', 5)

或将表格更改为默认值

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