当我在3个表中成功运行此代码数据并在第3个表插入查询中给出错误时提交,然后在第1个和第2个表中插入数据,而不是在第3个表中插入,也没有回滚功能正常运行。当第3个表查询不起作用时,我想这样做,那么第1和第2个表插入数据将被删除和删除。
$this->db->trans_start(TRUE);
// 1st table insert query
$this->db->insert('users',$userInfo);
$userId = $this->db->insert_id();
$query = $this->db->query("SELECT `roleId` FROM `role` WHERE roleName='Clint';");
foreach ($query->result_array() as $row)
{ $roleId = $row['roleId']; }
$user_role = array( 'user_id' => $userId, 'role_id' => $roleId );
// 2nd table insert query
$this->db->insert('user_role', $user_role);
$city_id += [ "projectInfo" => $userId ];
// 3rd table insert query
$this->db->insert('project', $projectInfo);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE )
{
echo "Flase";
$this->db->trans_rollback();
}
else
{
echo "True";
$this->db->trans_rollback();
}
答案 0 :(得分:0)
您正在手动回滚事务!在这种情况下,您应该使用
$ this-> db-> trans_begin();不是$ this-> db-> trans_start();
CI会自动回滚事务,您不需要手动进行。 如果使用$ this-> db-> trans_start,则不需要$ this-> db-> trans_rollback()。
注释掉$ this-> db-> trans_rollback(),或保留这些并使用$ this-> db-> trans_begin(),然后检查结果。
https://www.codeigniter.com/user_guide/database/transactions.html
祝你好运!