Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Insert Mysql & db_insert_id


andrey_kong

Recommended Posts

one of my customer complianed about my shop has bugs, which switches their shopping cart product to other items...

 

so i start to investigate the problem.

 

I found out that many of my orders_products are refering to non-exist order_id in the database.

 

This was because i put some hit log codes on each pages header, and those codes does a mysql_insert. I found out that many of the orders_products are refereing to those log's id, which is totally wrong.

 

I guess the problem was OScommerce using $insert_id = tep_db_insert_id(); , where insert_id was refering to the last mysql_insert id, which they thinks must be the orders row which just inserted. But sometime the last insert is my hit log rows...

 

So i changed to log to files base. and this problem sloved.

 

My question is, will this problem appear again if a lot people hitting my site? this tep_db_insert_id() function is session base? or script base? or db base?

 

PS i forgot to tell this $insert_id = tep_db_insert_id(); was found in paypal_ipn.php which is the Paypal IPN moudle.

Link to comment
Share on other sites

My question is, will this problem appear again if a lot people hitting my site? this tep_db_insert_id() function is session base? or script base? or db base?

 

http://dev.mysql.com/doc/mysql/en/information-functions.html

 

Look at the LAST_INSERT_ID() command halfway down.

 

Quote:

The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions.

 

Believe it if you want - I don't quite know what they mean by "client" in this case, but I assume it is an internal processID of some sort, and I assume MySQL is thread-safe.

I think as long as you retrieve the id immediately after an insert, you should be ok.

Link to comment
Share on other sites

Ive seen it happen before, I think it comes from mixing native mysql with tep mysql.

 

So if you did something like this.

 

 

tep mysql connect #1

 

mysql connect #2

insert hit log data

 

tep insert order

 

then

 

tep_db_insert_id() will give the ID from connectin 2 / hit log insert

 

I loked at the functin and think it should probably be like this but decided its better not to make two connectins.

 

function tep_db_insert_id() {
 return mysql_insert_id();
}

function tep_db_insert_id() {
 global $$link;
 return mysql_insert_id($$link);
}

 

It could also happen like this

 

 

tep mysql connect #1

 

tep insert hit log data

 

tep insert order (failed)

 

then

 

tep_db_insert_id() will give the ID from hit log insert

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...