2014-12-26 87 views
1

我有两个表,其中用户是admin,其信息存储在用户表中,学生信息存储在用户以及学生表中,所以当学生更新其基于id字段的学生表表如何更新用户表在codeigniter中同时更新两个表

users 

id | email  |password |firstname |user_type 
1 [email protected]  dfdf  a   0 //(user) 
2 [email protected]  dgg   g  1//(student) 
3 [email protected]  fjh   dd   1//(student)  

students 
id | email  |firstname |lastname| user_id 
2 [email protected] dgg  g   2 
3 [email protected]  fgh  dd   3 

与此更新查询

function update($student_id,$data) 
    { 

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

回答

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

只是一个侧面说明:这是最好的避免重复的内容,如“姓名”一栏。也许你可以找到另一种方式来做到没有重复的内容。

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); 

}