Following is my query in which I am making updates in two different tables. My question is will the following code work if the insertion for the first table or the second one fails or the like and the query gets roll back. Or there are any issues with the code. Kindly help this is he first time I am working with transactions.
Note: Assuming Variables are already assigned.
try {
// First of all, let's begin a transaction
$conect->autocommit(FALSE);
$wadb_stmt = $connect->prepare("UPDATE lecture_title SET video = 'how to code' WHERE id = ?");
$wadb_stmt->bind_param("s", $Lec_id); // s means only string input is allowed
if ($wadb_stmt->execute())
{
$wadb_stmt = $con->prepare("UPDATE lecture SET category = 'code' WHERE id = ?");
$wadb_stmt->bind_param("s", $code); /// s means only string input is allowed
if ($wadb_stmt->execute())
{
echo "true";
$connect->commit();
}
else echo "false";
}
else echo "false";
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$connect->rollback();
}