php - codeigniter transact 将数据添加到两个表中

标签 php database codeigniter transactions

嗨,我在将数据添加到两个表中时遇到问题,情况是这样的,我想在用户表中添加一个学生及其用户名和密码,并将他的姓名和年龄等个人信息添加到 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/

相关文章:

php - 如何从字段 auto_increment 填充字段外键

php - CodeIgniter MySQL 对不同行进行计数

php - 在 Wordpress 中覆盖父主题功能

php - 生成一个随机数以在后续页面中使用

database - 将代码与用户创建的数据库条目映射的首选方式

database - 通过JDBC合并来自不同数据库的表

java - 尝试将数据添加到数据库

javascript - AngularJS 加载个人用户配置文件

php - Highslide Gallery 单击缩略图后加载图像(提高页面速度)

php - 检查是否从地址栏或提交的表单访问页面