What is the common issue with displaying a limited number of entries per page in a PHP guestbook?

The common issue with displaying a limited number of entries per page in a PHP guestbook is that the pagination may not work correctly if the total number of entries is not considered. To solve this issue, you need to calculate the total number of entries and adjust the pagination logic accordingly.

// Assuming $entriesPerPage is the number of entries to display per page
// Assuming $currentPage is the current page number

// Query to get total number of entries
$totalEntries = $pdo->query("SELECT COUNT(*) FROM guestbook")->fetchColumn();

// Calculate the total number of pages
$totalPages = ceil($totalEntries / $entriesPerPage);

// Adjust current page if it exceeds total pages
if ($currentPage > $totalPages) {
    $currentPage = $totalPages;
}

// Calculate the offset for the SQL query
$offset = ($currentPage - 1) * $entriesPerPage;

// Query to fetch entries for the current page
$stmt = $pdo->prepare("SELECT * FROM guestbook ORDER BY id DESC LIMIT :offset, :entriesPerPage");
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->bindParam(':entriesPerPage', $entriesPerPage, PDO::PARAM_INT);
$stmt->execute();

// Display entries
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Display entry
}