php - 事务在 CodeIgniter 中无法正常工作

标签 php mysql codeigniter

    $debit_array = $this->input->post('debit[]');
    $credit_array = $this->input->post('credit[]');

    if(array_sum($debit_array) == array_sum($credit_array)){
        $this->db->trans_start();
        $this->db->where('Entry_id',$this->input->post('entry_id'));
        $this->db->delete('mk_account_transaction_master');

        $uniqueid = uniqid();
        $uniquecode = "Upadte Entry-".array_sum($debit_array)."-".$uniqueid;


    //  $this->db->trans_start();
        for($i=0;$i<sizeof($debit_array);$i++){

            $insert_data = array(
                'account_date' => date('Y-m-d',strtotime($this->input->post('date'))),
                'books' => $this->input->post('branch'),
                'account' => $this->input->post('account['.$i.']'),
                'debit' => $this->input->post('debit['.$i.']'),
                'credit' =>$this->input->post('credit['.$i.']'),
                'remarks' =>$this->input->post('remarks['.$i.']'),
                'updated_by' => $this->session->userdata(''),
                'entry_id' => $uniquecode,
                'display_item_id' => $disp1_id,
                'type' => $acc1_type,
                'group_id' => $grp1_id,
                'CF_Type' => $cf1_type,
                'Last_update_time' => date('Y-m-d H:m:s'),
            );
            $this->db->insert('mk_account_transaction_master',$insert_data);
        }
        $this->db->trans_complete();
    return 1;

我可以使用 trans_start() 和 trans_complete()。上面的函数会先删除一些数据,然后再插入一些数据。如果插入代码引发任何数据库错误,删除查询将回滚,但我的代码删除功能不会回滚

'updated_by' => $this->session->userdata('') 在此行中,updated_by 列被分配了 Null 值,因此 Sql 引发错误

"Updated_by column can not be null"

因此我想回滚已删除的数据,使用事务。但是不会回滚已删除的数据。

为什么交易不能正常工作?

最佳答案

代码是正确的。但是我使用了 MyISAM 表,所以事务不会影响这些类型的表。所以我将 MyISAM 表的类型更改为 InnoDb

现在效果很好

关于php - 事务在 CodeIgniter 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129952/

相关文章:

mysql - 根据mysql中的courseid获取特定列的计数

php - 使用 codeigniters 事件记录插入非文字值

php - 警告 : mysql_num_rows(): supplied argument is not a valid

mysql - 连接两个表时在mysql中查找重复值

php - 配置文件 View 更新

php - 使用 php 和 mysql 处理银行对账

php - CodeIgniter 路径常量定义

php - 通过 jQuery 提交表单后 View 未加载

php - 从数据库中检索下拉选项

php - 如何为 Bootstrap 使用/编译 LESS?