Мне интересно, можно ли объединить несколько команд вставки PDO в одну функцию, которая либо завершится ошибкой, либо зафиксируется после успешной обработки. Я опасаюсь, что информация может выйти из строя в середине вставки, поэтому у меня есть проверки, чтобы увидеть, был ли выполнен код. Это сейчас то, что у меня есть.
//all variables come from POST
$tacos = [
'name' => $name,
'ingredients' => $numIng,
];
$sql = "INSERT INTO tacos (name, ingredients) VALUES (:name, :ingredients)";
$stmt= $pdo->prepare($sql);
$tacoSave = $stmt->execute($tacos);
if ($tacoSave){
$tacoId = $pdo->lastInsertId();
$tacoIngredients = [
'ingredientName' => $iName,
'quantity' => $qt,
'size' => $tSize,
'price' => $tPrice,
'taco_id' => $tacoId
];
$sql = "INSERT INTO ingredients (ingredientName, quantity, size, price, taco_id) VALUES (:ingredientName, :quantity, :size, :price, taco_id)";
$stmt= $pdo->prepare($sql);
$ingredientSave = $stmt->execute($tacoIngredients);
if($ingredientSave){
$ingredients_id = $pdo->lastInsertId();
$ingredientsList = [
array(
'origin' => $iOrigin,
'picture' => $Img,
'ingredient_id' => $ingredients_id
),
array(
'origin' => $iOrigin2,
'picture' => $Img2,
'ingredient_id' => $ingredients_id
)
];
$sql = "INSERT INTO ingredients_origin (origin, picture, ingredient_id) VALUES (:origin, :picture, :ingredient_id)";
$stmt= $pdo->prepare($sql);
foreach ($ingredientsList as $ingredient)
{
$stmt->execute($ingredient);
}
}
}