function get_book_or_404() { $book = find_book_by_id(filter_var(params('id'), FILTER_VALIDATE_INT)); if (is_null($book)) { halt(NOT_FOUND, "This book doesn't exist."); } return $book; }
<?php require_once "../includes/session.php"; require_once "../includes/db_connection.php"; require_once "../includes/functions.php"; confirm_logged_in(); ?> <?php $current_book = find_book_by_id($_GET["book"], false); if (!$current_book) { // book ID was missing or invalid or // book couldn't be found in database redirect_to("manage_content.php"); } $id = $current_book["id"]; $query = "DELETE FROM yb_books WHERE id = {$id} LIMIT 1"; $result = mysqli_query($connection, $query); if ($result && mysqli_affected_rows($connection) == 1) { // Success $_SESSION["message"] = "book deleted."; redirect_to("manage_content.php"); } else { // Failure $_SESSION["message"] = "book deletion failed."; redirect_to("manage_content.php?book={$id}"); }
/** * This will select data for the current book or current category * @param */ function find_selected_book($public = false) { global $current_category; global $current_book; if (isset($_GET["category"])) { $current_category = find_category_by_id($_GET["category"], $public); if ($current_category && $public) { $current_book = find_default_book_for_category($current_category["id"]); } else { $current_book = null; } } elseif (isset($_GET["book"])) { $current_category = null; $current_book = find_book_by_id($_GET["book"], $public); } else { $current_category = null; $current_book = null; } }