Answer the question
In order to leave comments, you need to log in
Using lastInsertId() inside a transaction, can you be sure that the ID will not be taken from another transaction in the global sense?
$db->beginTransaction();
/*****/
$sql = "INSERT INTO products (name, price) VALUES (:name, :price)";
$sth = $db->prepare($sql);
$sth->execute(
array(
':name'=>$product['main']['name'],
':price'=>$product['main']['price'],
)
);
// Получаем id последней вставленной записи
$idProduct = $db->lastInsertId();
/*****/
foreach ($product['attributes'] as $attribute) {
$sql = "INSERT INTO productAttribute (idProduct, nameAttribute) VALUES (:idProduct, :nameAttribute)";
$sth = $db->prepare($sql);
$sth->execute(
array(
':idProduct'=>(int)$idProduct,
':nameAttribute'=>$attribute['attribute'],
)
);
}
Answer the question
In order to leave comments, you need to log in
lastInsertId() is unique per table
Why is PDO::lastInsertId returning the wrong ID on frequent database inserts?
Yes, for mysql it is possible.
https://dev.mysql.com/doc/refman/5.7/en/mysql-inse...
That is, the last generated ID in the current connection is taken.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question