Welcome Guest, Not a member yet? Register   Sign In
Insert Data into 2 Tables at the Same Time
#1

[eluser]Sean Cannon[/eluser]
My problem is pretty simple. In my view I have a form to insert data. The data needs to go into two separate tables.

Here is my controller function:
Code:
function insert()
    {
        $peopleInfo = array(
            'person_title' => $this->input->post('person_title'),
            'person_firstname' => $this->input->post('person_firstname'),
            'person_lastname' => $this->input->post('person_lastname'),
            'person_suffix' => $this->input->post('person_suffix'),
            'person_gender' => $this->input->post('person_gender'),
            'person_agerange' => $this->input->post('person_agerange'),
            'person_birthdate' => $this->input->post('person_birthdate'),
            'person_maritalstatus' => $this->input->post('person_maritalstatus'),
            'person_anniversary' => $this->input->post('person_anniversary'),
            'person_address' => $this->input->post('person_address'),
            'person_city' => $this->input->post('person_city'),
            'person_state' => $this->input->post('person_state'),
            'person_zipcode' => $this->input->post('person_zipcode'),
            'person_mailaddress' => $this->input->post('person_mailaddress'),
            'person_mailcity' => $this->input->post('person_mailcity'),
            'person_mailstate' => $this->input->post('person_mailstate'),
            'person_mailzipcode' => $this->input->post('person_mailzipcode'),
            );
        
        $memberInfo = array(
            'person_id' =>  , //THIS IS WHERE I AM STUCK!!
            'member_type' => $this->input->post('member_type'),
            'member_startdate' => $this->input->post('member_startdate'),
            'member_joinedhow' => $this->input->post('member_joinedhow'),
            );

            if ($this->people->addPeople($peopleInfo,$memberInfo))
            {
                redirect('people');
            }

My model function simply looks like this:

Code:
function addPeople($peopleInfo,$memberInfo)
    {
        $this->db->insert('nx_people', $peopleInfo);
        $this->db->insert('nx_membership', $memberInfo);
        return TRUE;
    }

What I can't figure out is if I can grab the person_id that has just been inserted so that I can use it in the nx_membership table. Is it possible to do this? Any tips are greatly appreciated!
#2

[eluser]narkaT[/eluser]
you can use $this->db->insert_id() to obtain the last auto_increment-value.
#3

[eluser]Sean Cannon[/eluser]
Hmmm...tried that and it always seems to be returning 0.

Code:
'person_id' =>  $this->db->insert_id(), //THIS IS WHERE I AM STUCK!!

Perhaps I am doing something wrong??
#4

[eluser]narkaT[/eluser]
you have to call this method after inserting the data into the first table Wink
#5

[eluser]Sean Cannon[/eluser]
Duh. Serious brainfart. :cheese:

I'm sure it'll work perfectly...will test it later, I have to work on another project for awhile. Thanks for your help!!!




Theme © iAndrew 2016 - Forum software by © MyBB