代码点火器事务 将数据添加到两个表中


codeigniter transact add data into two table

嗨,我在将数据添加到两个表中时遇到问题,情况是这样的,我想在用户表中添加一个带有用户名和密码的学生,以及他的姓名和年龄等个人信息到users_profiles,这是我的代码:

函数add_user($username,$password,$fname,$lname,$,$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
);