Welcome Guest, Not a member yet? Register   Sign In
Auto creating a database record and using that records ID throughout the app.
#1

[eluser]Unknown[/eluser]
Hello,
I'm trying to create an application that when the user first arrives at the main viewer they are asked to input their name and click next.
When they click next a new row is added to a database and the page refreshes to a new view that asks another question. This question needs to be added to the same row as the first question.

My problem is getting the $ID of the the record that was created in the first screen. I don't understand how to create a record to the database then share that records ID through out each controller/view for the remainder of the users experience in the application.

I'm sorry if this is very basic as I new to CI

Thanks
#2

[eluser]guidorossi[/eluser]
It's the mysql_insert_id() function to get the last insert id.

It's $this->db->insert_id() from the query helper functions on CodeIgniter
#3

[eluser]LuckyFella73[/eluser]
Additionally to what guidorossi posted you can use the
session class and store the insert_id into a session variable.
That way you can access the database id all the way long.
I allways use database session, have a look at the user guide
to get the sql code and to see how to enable DB sessions.
#4

[eluser]Unknown[/eluser]
I'm having problems with the insert_id() tag reporting 0 each time I call it.
I've read several other posts on here about the same issue and I really think my problem is my poor implementation
of said code.

I've not looked into the session class option yet as it looks like it would help a lot. I'm trying to carry this id across several different
controllers "sharing the variable" so I can update the table row with the data entered in each view.

I hope this makes sense. I feel this should be fairly simple but just can't crack this.

thanks,
#5

[eluser]guidorossi[/eluser]
You should use it like this:

Code:
$inserdata = array(
                       'tipe' => $this->input->post('tipe'),
                       'descr' => $this->input->post('descr'),
                       'title' => $this->input->post('title')
                    );

                    $this->db->insert('product', $inserdata);
                    
                    $insert_id = $this->db->insert_id();
#6

[eluser]tonanbarbarian[/eluser]
i would probably also test to see if the insert is working
Code:
$inserdata = array(
'tipe' => $this->input->post('tipe'),
'descr' => $this->input->post('descr'),
'title' => $this->input->post('title')
);

if (!$this->db->insert('product', $inserdata)) {
  echo 'error';
  return;
}

$insert_id = $this->db->insert_id();
it is possible that your insert code is failing and that is why the insert_id is returning 0
#7

[eluser]nuwanda[/eluser]
Yup, insert will return 0 if the insert failed.




Theme © iAndrew 2016 - Forum software by © MyBB