Welcome Guest, Not a member yet? Register   Sign In
Query Problem - Model
#1

[eluser]Unknown[/eluser]
Hello guys!
I need some help with my code.

I have this code in my model...
The function inserts successfully the "quote" but it's not return the "id" of the inserted quote.

Code:
function add_quote($customer_id, $contact_id, $comments, $payment_type, $delivery_type)
    {
        $user_id = $this->session->userdata('user_id');
        $ahora =  date('Y-m-d H:i:s');
        $data = array(
            'creation_date' => $ahora,
            'status_id' => 1,
            'user_id' => $user_id,
            'customer_id' => $customer_id,
            'contact_id' => $contact_id,
            'comments' => $comments,
            'payment_type' => $payment_type,
            'delivery_type' => $delivery_type
        );
        
        $add_quote_result = $this->db->insert('quotes', $data);

// Here comes the problem. $add_quote_result returns 1. The quote was inserted successfully but the code below seems not to work.
        
        $this->db->select('id');
        $this->db->from('quotes');
        $this->db->where($data);
        $get_quote_id = $this->db->get();
        $quote_id = $get_quote_id->result();

        
        return $quote_id;
}

what i'm doing wrong?

thanks in advance!
#2

[eluser]toopay[/eluser]
use '$this->db->insert_id()'...
Code:
function add_quote($customer_id, $contact_id, $comments, $payment_type, $delivery_type)
    {
        $user_id = $this->session->userdata('user_id');
        $ahora =  date('Y-m-d H:i:s');
        $data = array(
            'creation_date' => $ahora,
            'status_id' => 1,
            'user_id' => $user_id,
            'customer_id' => $customer_id,
            'contact_id' => $contact_id,
            'comments' => $comments,
            'payment_type' => $payment_type,
            'delivery_type' => $delivery_type
        );
        
        $this->db->insert('quotes', $data);
        return $this->db->insert_id();
        /* No need to do this, if you only need the id of the last inserted query.

// Here comes the problem. $add_quote_result returns 1. The quote was inserted successfully but the code below seems not to work.
        
        $this->db->select('id');
        $this->db->from('quotes');
        $this->db->where($data);
        $get_quote_id = $this->db->get();
        $quote_id = $get_quote_id->result();

        
        return $quote_id;
        */
}
#3

[eluser]Unknown[/eluser]
thanks toopay!!
it's solves my problem!!




Theme © iAndrew 2016 - Forum software by © MyBB