簡體   English   中英

在codeigniter中同時更新兩個表

[英]update two tables at same time in 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);
    }

您不能使用一個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”列。 也許您可以找到另一種沒有重復內容的方法。

請嘗試這個


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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM