Transactions Not working properly in insert query

Zakaria Sichaib :

So i'm trying to run an query by using a transaction but it's not working somehow i did check all the posts but no fixes seems to be working ! whenever i remove the transaction the insert query works just fine.

Here's the controller :

public function addEmployee(){
        $field = array(
            'NomClient'=>$this->input->post('fullName'),
            'TelClient'=>$this->input->post('tel'),
            'WilayaClient'=>$this->input->post('wilaya'),
            'CommuneClient'=>$this->input->post('commune'),
            'AdresseClient'=>$this->input->post('adresse'),
            'StatusID'=>$this->input->post('statusCommande'),
            'TelevendeuseID'=>$this->input->post('televendeuse')
            );
        $result= $this->m->addEmployee($field);
        $msg['success'] = false;
        $msg['type'] = 'add';
        if($result){
            $msg['success'] = true;
        }
        echo json_encode($msg);

    }

My model :

public function addEmployee($field){

         $this->db->trans_start();
         return $this->db->insert('Clients',$field);
         $this->db->trans_complete();

    }

Please note that when i switch the model to :

    public function addEmployee($field){


         return $this->db->insert('Clients',$field);


    }

The record gets inserted successfully ! Which means that something is wrong with the transaction. I'm currently using one query to test if it's working so i can use multiples onces after that. Note that the tables are InnoDB tables so the problem isn't with the table type. Please help me out !

Nigel Ren :

When you call return it will end the function, so when it hits

return $this->db->insert('Clients',$field);

it will not get to

$this->db->trans_complete();

so perhaps...

$this->db->trans_start();
$return = $this->db->insert('Clients',$field);
$this->db->trans_complete();

return $return;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386398&siteId=1