CodeIgniter Forums
trans_status wrong value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: trans_status wrong value (/thread-71901.html)



trans_status wrong value - geekita - 10-06-2018

PHP Code:
$this->db->trans_start();
{
    $query_str "SELECT MAX(id) max_val FROM c WHERE i = ? ";

    $query $this->db->query($query_str, array($c['i']));
    $row $query->row_array();
    
    $id 
1;
    
    if 
($row)
    {
        $id $row['max_val'] + 1;
    }
    
    $c
['id'] = $id;
    
    $this
->db->set('t''NOW()'FALSE);
    $this->db->insert('c'$c);
}
$this->db->trans_complete();

return 
$this->db->trans_status() === FALSE FALSE $c['id']; 
Hi! I'm using the above code to read the max id from a table and then add a new row with maxid + 1. I call this piece of code via an ajax call and it works the very first time. If I call it a second time, trans_status equals FALSE. This is so weird because if I refresh the whole page via F5 it works every time. What am I missing? Huh  I'm using CI 3.1.9 + Mysql InnoDB 5.6.20 (mysqli driver).
Thanks!


RE: trans_status wrong value - geekita - 10-07-2018

The problem pops out when stricton => TRUE. I was trying to insert an empty string into an int column. What is still obscure to me is the first time I load the page and submit data via ajax, $data['x'] equals NULL. If I re-submit same data without refreshing page, $data['x'] equals "" (empty string).

This is how I get data from post


PHP Code:
$data['x'] = $this->input->post('x'); 



RE: trans_status wrong value - php_rocs - 10-08-2018

@geekita,
Why are you doing this? All you have to do is configure your database table correctly and it will automatically do this for you. You are adding an extra layer of code where you do not need to.