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

标签 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_型号:

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

数据库:

 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)

没关系。

最佳答案

您需要插入一些订单值

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

关于php - 我无法将数据插入数据库(codeigniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759716/

相关文章:

codeigniter - 限制 codeigniter 管理面板的 ip 地址

PHP - session_id() 给我 HTTP/1.1 500 内部服务器错误

php - mysql连接查询有很多字段

php - 需要解析ini文件来提取值

php - 函数 mcrypt_encrypt() 在 Wamp 中不起作用(PHP 版本 5.2.6)

php - 我如何正确创建一个 PHP 数组以便稍后循环并获取每条信息?

sqlite - 在 codeigniter 中使用 SQLite

mysql - 使用索引优化带有 ORDER BY 的 SELECT 语句

PHP MYSQL Select where 多个变量

php - code igniter - 如何在我的数据库 mysql/php 中保存图像