PHP mysqli ignores second insert to the different table (mysqli_prepare, mysqli_stmt_bind_param used)

  mysqli, php

I need to insert data to the different tables. If first insert was successful, than i can insert second entity. Without transaction this code inserts only first (book) record. How can i fix this code to insert both of them, book and userBook?

$success = FALSE;
    $connection = DB::createConnection();

    $sqlInsertBook = "INSERT INTO `book`(`bookId`, `name`, `author`, `description`, `bookCoverImage`) 
                VALUES (NULL,?,?,?,?)";
    $sqlInsertReview = "INSERT INTO `userBook`(`userBookId`, `userId`, `bookId`, `dateRead`, `grade`, `comment`, `note`) 
            VALUES (NULL,?,?,?,?,?,?)";

    mysqli_begin_transaction($connection);
    try {
        $stmt = mysqli_prepare($connection, $sqlInsertBook);
        mysqli_stmt_bind_param($stmt, "ssss", $bookName, $bookAuthor, $bookDescription, $bookCover);
        $success = mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);

        $bookId = mysqli_insert_id($connection);

        if (isset($bookId)) {
            $stmt = mysqli_prepare($connection, $sqlInsertReview);
            mysqli_stmt_bind_param($stmt, "ississ", $userId, $bookId, $dateRead, $grade, $comment, $note);
            $successReview = mysqli_stmt_execute($stmt);
            $success =  $success && $successReview;

            mysqli_stmt_close($stmt);
            mysqli_commit($connection);
        } else {
            mysqli_rollback($connection);
        }
    } catch (mysqli_sql_exception $exception) {
        mysqli_rollback($connection);
    } finally {
        mysqli_close($connection);
    }
    return $success;

Source: Ask PHP

LEAVE A COMMENT