Codeigniter 3 博客应用程序错误:update_post 函数修改 created_at 数据库 table 列
Codeigniter 3 blogging application bug: update_post function modifies created_at database table column
我正在使用 Codeigniter 3.1.8 和 Bootstrap 4.[=18= 开发一个基本的博客应用程序]
在我的 Posts_model 模型中,我有这段代码负责在数据库中插入一个新的 post:
public function create_post($post_image, $slug) {
$data = [
'title' => $this->input->post('title'),
'slug' => $slug,
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'author_id' => $this->session->userdata('user_id'),
'cat_id' => $this->input->post('category'),
'created_at' => date('Y-m-d H:i:s')
];
return $this->db->insert('posts', $data);
}
此函数在帖子控制器中使用:$this->Posts_model->create_post($post_image, $slug);
.
更新机制类似。在我的 Posts_model 模型中:
public function update_post($id, $post_image, $slug) {
$data = [
'title' => $this->input->post('title'),
'slug' => $slug,
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'cat_id' => $this->input->post('category'),
'updated_at' => date('Y-m-d H:i:s')
];
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
上述函数在 Posts 控制器中使用如下:$this->Posts_model->update_post($id, $post_image, $slug);
.
问题
出于某种原因,我一直无法识别,每当我 更新一个post时,created_at posts
table 的列也被更新。换句话说,列 created_at 和 updated_at 具有相同的值。为什么会这样?
只需运行 phpmyadmin 中的这些查询
在这里将 created_at
的数据类型从 timestamp
更改为 datetime
并将默认值指定为 null
或 none
你可以给任何你想要的,我在这里设置 NULL
ALTER TABLE `posts` CHANGE `created_at` `created_at` DATETIME NULL DEFAULT NULL;
我正在使用 Codeigniter 3.1.8 和 Bootstrap 4.[=18= 开发一个基本的博客应用程序]
在我的 Posts_model 模型中,我有这段代码负责在数据库中插入一个新的 post:
public function create_post($post_image, $slug) {
$data = [
'title' => $this->input->post('title'),
'slug' => $slug,
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'author_id' => $this->session->userdata('user_id'),
'cat_id' => $this->input->post('category'),
'created_at' => date('Y-m-d H:i:s')
];
return $this->db->insert('posts', $data);
}
此函数在帖子控制器中使用:$this->Posts_model->create_post($post_image, $slug);
.
更新机制类似。在我的 Posts_model 模型中:
public function update_post($id, $post_image, $slug) {
$data = [
'title' => $this->input->post('title'),
'slug' => $slug,
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'cat_id' => $this->input->post('category'),
'updated_at' => date('Y-m-d H:i:s')
];
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
上述函数在 Posts 控制器中使用如下:$this->Posts_model->update_post($id, $post_image, $slug);
.
问题
出于某种原因,我一直无法识别,每当我 更新一个post时,created_at posts
table 的列也被更新。换句话说,列 created_at 和 updated_at 具有相同的值。为什么会这样?
只需运行 phpmyadmin 中的这些查询
在这里将 created_at
的数据类型从 timestamp
更改为 datetime
并将默认值指定为 null
或 none
你可以给任何你想要的,我在这里设置 NULL
ALTER TABLE `posts` CHANGE `created_at` `created_at` DATETIME NULL DEFAULT NULL;