【发布时间】:2018-02-16 06:21:24
【问题描述】:
我的数据库中有一个 users 表,我想与另外两个表(cmets、posts)建立关系。
当我发表评论时,它会根据 id 及其用户名发布;帖子也是如此,如果用户发布帖子,其用户名进入数据库,现在假设该用户想要更新他的信息,例如他的电子邮件、网站、头像 img 等。
其他两个表中的所有新数据都应根据 users.id/user_id 自行更新。我确定这与加入有关,我实际上正在这样做,但什么也没发生。
这是我的代码:
public function update($id, $data)
{
$this->db->select('users.*');
$this->db->from('users');
$this->db->join('posts', 'posts.username = users.username','inner');
$this->db->join('posts', 'posts.user_id = users.id','inner');
$this->db->join('comments', 'comments.user_id = users.id','inner');
$this->db->join('comments', 'comments.username = users.username','inner');
$this->db->join('comments', 'comments.avatar_img = users.avatar_img','inner');
$this->db->join('comments', 'comments.email = users.email','inner');
$this->db->join('comments', 'comments.website = users.website','inner');
// Update info in all tables according to user id.
$this->db->where('id', $id);
$query = $this->db->get();
$this->db->update($this->table, $data);
}
您可以在上面的代码中观察到,我正在尝试从用户表中选择所有内容,并且我正在尝试将其与另一个表连接起来以更新其信息。
希望大家能帮帮我,谢谢。
【问题讨论】:
-
我很好奇,那有什么作用?你必须让“某事”发生!
-
$data 里有什么?你能分享一个例子吗?为什么还要在执行更新时执行 Select 查询?
-
单独更新每个表数据
标签: php codeigniter join codeigniter-3