vendredi 24 juin 2016

Codeigniter model not saving on database

I have an application that should save every transaction on my database. The controller code goes like this: $info = array( 'student_id' => $this->user['id'], 'transaction_id' => $ref, 'value' => $amount, 'final_value' => $final_value, 'payment_status' => '0', 'date' => date('Y-m-d'), ); $this->load->model('student_model'); if($this->student_model->insertPurchaseForStudent($info)){ // Some other code here } And this is my model ('student_model'): public function insertPurchaseForStudent($info){ if($this->db->insert('compra', $this->db->escape($info))){ return true; }else{ return false; } } I'm having an enormous headache because some transactions are saved on the database and others are not. I thought it could be some kind of instability on my database servers, so I've added a for loop, to try more than once, and to log an error message if it's not able to save: $i = 1; for ($i == 1; $i <= 5; $i++) { if($this->student_model->insertPurchaseForStudent($info)){ // to leave the loop $i = 7; // Some other code here }else{ sleep(3); switch($i){ case 2 : log_message('error', 'Second attempt to add transaction'); break; case 3 : log_message('error', 'Third attempt to add transaction'); break; case 4 : log_message('error', 'Fourth attempt to add transaction'); break; case 5 : log_message('error', 'Fifth attempt to add transaction'); break; case 6 : log_message('error', 'Sixth attempt, giving up'); break; } } } The problem is, when it does not save the transaction, I don't see any messages on my error log. I'd be really grateful with any ideas of what's the possible cause, or where am I slipping in this code I wrote. Cheers.

Aucun commentaire:

Enregistrer un commentaire