Php 在codeigniter中同时更新两个表
我有两个表,其中user是admin,它的信息存储在users表中,students信息存储在users表中,同时也存储在students表中,所以当学生根据students表中的id字段更新其表时,如何更新users表Php 在codeigniter中同时更新两个表,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有两个表,其中user是admin,它的信息存储在users表中,students信息存储在users表中,同时也存储在students表中,所以当学生根据students表中的id字段更新其表时,如何更新users表 users id | email |password |firstname |user_type 1 abc@gmail.com dfdf a 0 //(user) 2 xd@gmail.com dg
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();
以下是文件:
请注意:最好避免重复内容,如“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);
}