Very strange behaviour occurring. I have an API in PHP that takes a transaction and performs an insert into a ITEMS table. The table has an auto incrementing ID field.
When I perform an INSERT I retrieve the lastInsertId which I use to perform a LogActivity entry.
There are several people using the APP. I noticed that in the LogActivity table, there are some entries with IDs but those IDs aren't in the ITEMS table.
How is it possible that I can retrieve the lastInsertId but that ID isn't there. It's like the INSERT was performed, but then backed out??
Is this a concurrency issue? The table is INNODB. I'm not using TRANSACTION or LOCK or TRY/CATCH.
Any ideas would be appreciated.
//products.php
class Product{
// database connection and table name
private $conn;
private $table_products = "table_products";
private $table_productitems = "table_productitems";
private $table_location = "warehouse_locations";
// object properties
public $id;
public $productname;
public $description;
public $quantity;
public $productid;
public $customerid;
public $thisid;
public $thisitemid;
public $productqty;
public $newproductid;
public $updateitemid;
public $updateqty;
public $type;
public $result;
public $insertitemid;
public $insertqty;
public $insertqrcode;
public $searchStr;
public $csproductname;
public $cssku;
public $csproductid;
// constructor with $db as database connection
public function __construct($db){
$this->conn = $db;
}
function addProductItemQRCode(){
// query to insert record
$query = "INSERT INTO table_productitems
SET productid = :productid, quantity = :productqty, qrcode = :qrcode, modifydate = now()";
// prepare query
$stmt = $this->conn->prepare($query);
// bind values
$stmt->bindParam(':productid', $this->insertitemid);
$stmt->bindParam(':productqty', $this->insertqty);
$stmt->bindParam(':qrcode', $this->insertqrcode);
// execute query
if($stmt->execute()){
$this->thisinsertid = $this->conn->lastInsertId();
} else {
$this->thisinsertid = '';
}
}
// Activity function
function logActivity($customerid,$type,$result,$quantity,$userid){
// query to insert record
$query = "INSERT INTO api_activity
SET customerid=:customerid, type=:type,
result=:result, quantity=:quantity, userid=:userid";
// prepare query
$stmt = $this->conn->prepare($query);
// bind values
$stmt->bindParam(':customerid', $customerid);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':result', $result);
$stmt->bindParam(':quantity', $quantity);
$stmt->bindParam(':userid', $userid);
// execute query
if($stmt->execute()){
return true;
}
return false;
}
// Addproductitems.php
// instantiate productitem insert object
$productiteminsert = new Product($db);
$productiteminsert->customerid = $post_customerid;
$productiteminsert->insertitemid = $productid->pid;
$productiteminsert->insertqty = $post_quantity;
$productiteminsert->insertqrcode = $post_qrcode;
// query products
$productiteminsert->addProductItemQRCode();
if($productiteminsert->thisinsertid!=null){
$newitem = $productiteminsert->thisinsertid;
// log the activity
$product->logActivity($customerid=$productiteminsert->customerid,
$type='add stock', $result=$productiteminsert->insertitemid.' ['.$newitem.'] ['.$productiteminsert->insertqrcode.']',
$quantity=$productiteminsert->insertqty, $userid=$userid);
}
The App posts to the Addproductitems.php.