- 1. Create a new HTML form named input.html that contains three input fields: itemName, itemDescription, and price.
- 2. Create a new PHP script named input.php that inserts those values into your Apache Derby database. Use search.php as your template.
- 3. Ensure that both of your INSERT statements either succeed, or fail, by performing the work inside a transaction.
INSERT statements
INSERT INTO menu.food(name, description) VALUES (:name, :description); -- IDENTITY_VAL_LOCAL() returns the last generated ID value INSERT INTO menu.prices (id, price) VALUES (IDENTITY_VAL_LOCAL(), :price);
PDO transactions
<?php
try {
// Begin a transaction
$conn->beginTransaction();
// Execute the statements, passing in an array of input variables
$stmt_food->execute(array(':name' => $name, ':description' => $description));
$stmt_price->execute(array(':price' => $price));
}
catch (PDOException $e) {
// Uh-oh -- roll back the transaction
$conn->rollBack();
print menu_footer();
exit();
}
// Commit the transaction
$conn->commit();
?>