Forums

how to get id of last database-insterted record?

seppo 06 Nov, 2018
Typically, one could use:
$conn = new mysqli($servername, $username, $password, $dbname);
and then later on:
$last_id = $conn->insert_id;
to get the id of last inserted record.

But how to do that with chronoforms, and then use it at next database-insert event?
I quess I could use custom code and write that $last_id = $conn->insert_id, but what to use instead conn, and would it work or is there some DB insert bu chronoforms itself in between?
Also, if I was to use such custom code, how then to get that $last_id to be used with next database save-method?
Thanks in advance!
GreyHead 06 Nov, 2018
Hi seppo,

Why do you need that? If the table is set in MySQL to auto-increment then it will automatically get the next id when you save the record.

Bob
seppo 06 Nov, 2018
To use save to database again, for relational purposes: I need to save parts of the form to another table, and set there the id of the first database record, thus to keep them connected.
Also if I could need that information for dynamic form: If I need to reference that already saved part i.e. at another page of multipage form.
healyhatman 06 Nov, 2018
Answer
1 Likes
It will be available as part of the save data action -> {var:save_data#.id} (if the column is called id, otherwise use whatever your column is called)
seppo 07 Nov, 2018
Thank you healyhatman, that was it!
This topic is locked and no more replies can be posted.