在codeigniter中同时更新两个表

时间:2014-12-26 01:59:32

标签: php mysql codeigniter

我有两个表,其中用户管理其信息存储在用户表中,学生信息存储在用户以及学生表中,因此当学生更新其表格时,基于学生表中的id字段如何更新用户表

users

id  | email     |password   |firstname    |user_type
1  abc@gmail.com     dfdf       a            0 //(user)
2  xd@gmail.com      dgg         g       1//(student)
3  sa@ggg.com        fjh         dd          1//(student)     

students
id | email      |firstname |lastname| user_id 
2   xd@gmail.com    dgg      g          2 
3   sa@ggg.com      fgh     dd            3 

和此更新查询

function update($student_id,$data)
    { 

        $this->db->where(array('id' => $student_id));
        return $this->db->update('students',$data);
    }

2 个答案:

答案 0 :(得分:0)

您无法使用一个SQL语句更新多个表。 Codeigniter确实允许您使用事务。看看:

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

以下是文件:
https://ellislab.com/codeigniter/user-guide/database/transactions.html

请注意:最好避免重复内容,例如“firstname”列。也许你可以在没有重复内容的情况下找到另一种方法。

答案 1 :(得分:0)

请试试这个


function update($student_id,$data)
{ 
    // update the student table first //
    $this->db->where(array('id' => $student_id));
    return $this->db->update('students',$data);

    //update the user table next//
    $this->db->where(array('id' => $student_id));
    return $this->db->update('users',$data); 

}