2014-12-26 165 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); 

}