function updateBooks($db, $bookId, $bookName, $bookNotes, $bookCollections, $bookAthors)
{
    //echo '<pre>'.print_r($bookCollections, true).'</pre>';
    //echo '<pre>'.print_r($bookAthors, true).'</pre>';
    if (strlen($bookName) <= 0 && $bookId <= 0 && (is_array($bookCollections) && is_array($bookAthors))) {
        return false;
    }
    // update book title and book notes
    if (!($stmtUpdate = $db->prepare("UPDATE books SET book_title = '" . $bookName . "', notes = " . ($bookNotes == "NULL" ? "NULL" : "'" . $bookNotes . "'") . " WHERE book_id=" . $bookId))) {
        echo "Prepare update with book_title and notes failed: (" . $db->errno . ") " . $db->error;
        return false;
    }
    if (!$stmtUpdate->execute()) {
        echo "Execute update book failed: (" . $stmtUpdate->errno . ") " . $stmtUpdate->error;
        return false;
    }
    if (count($bookCollections) > 0 || count($bookAthors) > 0) {
        // for update of relations in books_authors we need to delete old records and insert the new ones
        if (deleteRelationBookAuthorCollection($db, $bookId) === true) {
            $results = insertRelationBookAuthorCollection($db, $bookId, $bookAthors, $bookCollections);
            if (count($results) <= 0) {
                echo "Error on insert in books_authors.";
                return false;
            }
        } else {
            echo "Error on delete books_authors.";
            return false;
        }
    }
    return true;
}
Beispiel #2
0
            $selectedCollectionIds[] = (int) $multiCollectionId;
        }
    }
    if (count($errMsg) > 0) {
        foreach ($errMsg as $err) {
            echo $err . '</ br>';
        }
    } else {
        mysqli_autocommit($db, FALSE);
        $newBookId = insertBooks($db, $bookName, $bookNotes);
        if ($newBookId === false) {
            echo 'Грешка при въвеждане на книга!';
            mysqli_rollback($db);
        } else {
            $insertedIds = array();
            $insertedIds = insertRelationBookAuthorCollection($db, $newBookId, $selectedAuthorIds, $selectedCollectionIds);
            if (count($insertedIds) > 0 && $insertedIds !== false) {
                echo 'Успешен запис на книга.';
                mysqli_commit($db);
                mysqli_autocommit($db, TRUE);
            } else {
                echo 'Грешка!';
                mysqli_rollback($db);
                mysqli_autocommit($db, TRUE);
                header('Location: index.php');
                exit;
            }
        }
    }
}
?>