我有两个表,其中用户是管理员,其信息存储在用户表中,学生信息存储在用户和学生表中,所以当学生根据学生表中的 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
请注意:最好避免重复的内容,例如“名字”列。也许您可以找到另一种没有重复内容的方法。
关于php - 在 codeigniter 中同时更新两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652169/