gpt4 book ai didi

php - 如何清除 $this 以在 CodeIgniter 中插入更多记录?

转载 作者:行者123 更新时间:2023-11-29 01:58:08 24 4
gpt4 key购买 nike

使用 CI,我想在 User 表中插入一条记录,在 Post 表中插入一条记录。下面是我的代码的简要说明(两个表会有多个列,我只以一个为例)。

$this->username=$user;
$this->db->insert('User',$this);
$this->title='my first post';
$this->db->insert('Post', $this);

但是,第二个插入将类似于“插入 Post (user, title) values ('$user', 'my first post')。并报告错误,即 Post 中的未知列用户。

如何在插入下一条记录(在另一个表中)之前清除 $this 中的成员?

最佳答案

这是因为

$this->username=$user;

你可能需要使用

$this->db->insert('Post', $this->title);

在你插入之前,在标题中设置你想要的任何东西,但不是

$this->db->insert('Post', $this);

但是,如果您仍想使用某个对象,可以在此处找到有关如何正确执行此操作的更多信息,http://ellislab.com/codeigniter/user-guide/database/active_record.html#insert

关于php - 如何清除 $this 以在 CodeIgniter 中插入更多记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com