Not sure if this is the correct terminology, but I have 3 separate transactions to complete Below is skeleton of the code, and it works ok
$ok = TRUE;
$sql = "INSERT INTO `members`...";
$stmt = $conn->prepare($sql);
if (
$stmt &&
$stmt->bind_param(...) &&
$stmt->execute()) {
$memid = $stmt->insert_id;
mysqli_stmt_close($stmt);
$sql = "UPDATE `waitlist`...";
$stmt = $conn->prepare($sql);
if (
$stmt &&
$stmt->bind_param(...) &&
$stmt->execute()) {
mysqli_stmt_close($stmt);
$sql = "INSERT INTO `classmembers`...)";
if ($conn->query($sql)) {
$ok = TRUE;
} else {
die(mysqli_error($conn));
}
} else {
die(htmlspecialchars($conn->error));
}
} else {
die(htmlspecialchars($conn->error));
}
if ($ok) {
//further actions
header('location: home.php');
} else {
// INSERT INTO `classmembers will not have happened so can be ignored
// Delete from `members` is easy to do
// UPDATE `waitlist` is more problematic because there is a comments field that has been updated.
}
However is there a better way of rolling back the transactions if any fail other than the way indicated in the final “else” block?