Here is the scenario. This table Orders has an auto\_increment column called orders\_id. The table uses InnoDB storage engine. (This is not about inserting new records in the Orders table, its about how to avoid sending duplicate IDs to the processor)

```
orders_id       cust_id       name       address 
100              55           abc        123 street    
101              12           def        456 street   

```

For the next order, I calculate the next available Orders\_id like so

```
$new_order_id_query = "SHOW TABLE STATUS LIKE ORDERS";
$result = tep_db_query($new_order_id_query);
$row = tep_db_fetch_array($result);
$new_order_id = $row['Auto_increment'];

```

Then I collect other information like name, address, items\_ordered... and send it to the payment processor like this

```
102              44           xyz         44 Street

```

The problem is **only** sometimes when there are two or more orders at exactly the same time, I end up sending **two orders with the same orders\_id** to the payment processor.

The question is how can I make sure that I am not sending a duplicate Orders\_id to the Payment Processor?

I have been suggested to use last\_insert\_id() which may be right or wrong but I dont know how I can implement it. 

I read about Locking Tables - Is that a good idea for a Transaction Environment and InnoDB? If so how can I implement it?