Php Codeigniter 3博客应用程序错误:update_post函数修改数据库表列中创建的_
我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序 在我的Posts\u模型中,我有一段代码负责在数据库中插入一篇新文章:Php Codeigniter 3博客应用程序错误:update_post函数修改数据库表列中创建的_,php,codeigniter-3,Php,Codeigniter 3,我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序 在我的Posts\u模型中,我有一段代码负责在数据库中插入一篇新文章: public function create_post($post_image, $slug) { $data = [ 'title' => $this->input->post('title'), 'slug' => $slug, 'descript
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);
}
此功能用于Posts控制器:$This->Posts\u model->create\u post($post\u image,$slug)代码>
更新机制类似。在myPosts\u模型中模型:
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\u model->update\u post($id,$post\u image,$slug)代码>
问题
出于某种原因,我一直无法确定,每当我更新帖子时,posts
表的created_at列也会更新。换句话说,在处创建的列和在处更新的列具有相同的值。为什么会发生这种情况?只需在phpmyadmin中运行这些查询
这里将在
创建的的数据类型从时间戳
更改为日期时间
,并将默认值分配为null
或none
您可以给任何您想要的人,这里我设置null
ALTER TABLE `posts` CHANGE `created_at` `created_at` DATETIME NULL DEFAULT NULL;
如果在
处创建的的数据类型是时间戳
并且在更新当前时间戳时是,那么每次更改时它都会更新。@devsiodera我应该将其更改为什么?是否检查了字段类型?@devsiodera是的,它是当前时间戳
。我使用迁移来创建表。对于posts表,我有:'updated_at'=>数组('type'=>'TIMESTAMP',),
。我应该将其更改为什么?然后将其更改为mySQL级别的、类型
=>日期时间
和默认值
=>无
。但是移民呢?请看一下“原件”。您可以在迁移文件中更改相同的内容。请尝试将答案发布到。非常感谢。