Noah Goodrich
Noah Goodrich

Reputation: 315

Using $this->db->insert_id() with multiple users on Codeigniter

I'm building an eCommerce site with Codeigniter which will allow users to register, buy products and then track the orders.

I'm using the following in several places around the site, mainly when a user is submitting an order:

$this->db->insert_id();

Basically when a user submits an order, it will add the order to one table, and then, within the same segment of code (immediately after the insert query), add each order item to another table using the ID created when the order is inserted into the first table.

My question is: Out of the following, what does $this->db->insert_id(); do:

1) Does it get the ID that has just been inserted in (and only from) insert query just run? 2) Does it get the last inserted ID from the latest entry in the database regardless of what query its come from?

Basically I'm trying to avoid orders being mixed up, say for example if several customers were submitting orders at the same time, I don't want one customer's order items to be added to the incorrect order.

I think the answer is 1, and that there's no problem, but I wanted to be sure.

Thanks!

Upvotes: 1

Views: 1795

Answers (3)

bittids
bittids

Reputation: 150

Your question exposes a potential bug in the codeigniter environment. If two inserts are done in rapid succession, how do you have confidence that the ID returned from insert_id is the proper ID?

Codeigniter documentation does not answer this question

http://ellislab.com/codeigniter/user-guide/database/helpers.html

A relevant blog entry from ellis lab does not resolve the question. It concludes that the appropriate resolution is to take your chances.

http://ellislab.com/forums/viewthread/63052/

If this function is a wrapper function for mysqli_insert_id, the documentation at php.net is unclarified.

http://www.php.net/manual/en/mysqli.insert-id.php

It states the ID is from "the last query". It does not say whose last query.

Two successive inserts, and the return of a wrong ID will compromise the integrity of your data. The way to be sure is lock the database.

$this->db->query('LOCK TABLE (your table name) WRITE');
$this->db->insert('(your table name');
$int_id = $this->db->insert_id();
$this->db->query('UNLOCK TABLES');

This has a negative impact on execution time, but depending on your server's capacity is likely preferable to data corruption.

Upvotes: 0

cartalot
cartalot

Reputation: 3148

Just a suggestion - but another way to do this is to generate a random string - and use that to associate the cart items and order together - instead of by order id. you would still use the order id as the "order number".

this gives you the option of generating that random string when the shopping session first begins and using it to tie the cart items, shipping, billing etc together as the purchase is proceeding. so in that way you are starting the order immediately, but you haven't had to commit a space in the final order table until the transaction verifies.

Upvotes: 0

stuyam
stuyam

Reputation: 10049

It gets the ID that last inserted by the last query. So what you said in #1

Upvotes: 2

Related Questions