CodeIgniter Forums
Using $this->db->insert_id(); - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Using $this->db->insert_id(); (/thread-35502.html)



Using $this->db->insert_id(); - El Forum - 11-01-2010

[eluser]snowstar[/eluser]
Hi All,

Im having some issues using $this->db->insert_id();

i think this is mostly due to not knowing how to use it properly.

Basically im inserting data into two tables from one form, using $this->db->insert_id(); to recall the id used in the first array.

below is my controller and the error im getting.

can any one help point me in the right direction?

My controller
Code:
function add()
    {
        
        // Load the model
        $this->load->model('sales/sales');
        
        $data1 = array(
            'clientid' => $this->input->post('clientid'),
            'userid' => $this->input->post('userid'),
            'date' => $this->input->post('date'),
            'totalprice' => $this->input->post('totalprice'),
            'paymenttype' => $this->input->post('paymenttype'),
            'chequeid' => $this->input->post('chequeid'),
            'bank' => $this->input->post('bank'),
            'branch' => $this->input->post('branch'),
        );
        
        
        $this->sale_model->add_one($data1);
        
        $id = $this->db->insert_id();
        
        $data2 = array(
            'prodid' => $this->input->post('prodid'),
            'qty' => $this->input->post('qty'),
            $id => $this->input->post('orderid'),
        );
        
        $this->sale_model->add_two($data2);
        
        // Load View after submit
        $this->index();
    }

The Error
Quote:A Database Error Occurred

Error Number: 1054

Unknown column '123184' in 'field list'

INSERT INTO `mytable` (`prodid`, `qty`, `123184`) VALUES ('IG101', '1', 0)



Using $this->db->insert_id(); - El Forum - 11-01-2010

[eluser]snowstar[/eluser]
and two seconds after i post this i see where i went wrong

The fix
Code:
'orderid' => $id