douge3830 2011-05-17 04:06
浏览 28
已采纳

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

hi i have a problem about adding data into two tables, heres the situation, i want to add a student with his username and password in users table and his personal info like name and age into users_profiles,, heres my code:

function add_user($username,$password,$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();
}
  • 写回答

1条回答 默认 最新

  • dongzhi1949 2011-05-17 04:26
    关注

    You do not need to use get() when you use the query() method. Instead store the result from the query() method and use result() or row() to access the data.

    So your code should be something like:

    $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
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试