嗨,我在将数据添加到两个表中时遇到问题,情况是这样的,我想在用户表中添加一个学生及其用户名和密码,并将他的姓名和年龄等个人信息添加到 users_profiles 中,这是我的代码:
函数 add_user($用户名,$密码,$fname,$lname,$sex,$address,$city,$country,$role) { $this->db->trans_start();
$user = array(
'usrName'=>$username,
'usrPassword'=>sha1($password),
'roleID'=>$role
);
$this->db->insert('users', $user);
$this->db->query('SELECT usrID FROM users WHERE usrName=$username');
$usrID = $this->db->get(); //i know this is wrong thats why i need help
$user_profile = array(
'usrpFirstName'=>$fname,
'usrpLastName'=>$lname,
'usrpSex'=>$sex,
'usrpAddress'=>$address,
'usrpCity'=>$city,
'usrpState'=>$country,
'usrID'=>**$usrID** // this is the foreign key from users table
);
$this->db->insert('users_profiles', $user_profile);
$this->db->trans_complete();
}
最佳答案
当您使用query()
方法时,您不需要使用get()
。而是存储 query()
方法的结果并使用 result()
或 row()
访问数据。
所以你的代码应该是这样的:
$result = $this->db->query("SELECT usrID FROM users WHERE usrName=$username");
$row = $result->row();
$user_profile = array(
'usrpFirstName'=>$fname,
'usrpLastName'=>$lname,
'usrpSex'=>$sex,
'usrpAddress'=>$address,
'usrpCity'=>$city,
'usrpState'=>$country,
'usrID'=>$row->usrID
);
关于php - codeigniter transact 将数据添加到两个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026142/