代码拉取完成,页面将自动刷新
<?php
include("config.php");
// 处理删除请求
if (isset($_GET['delete_id']) && !empty($_GET['delete_id'])) {
$deleteId = intval($_GET['delete_id']);
// 执行删除操作
$deleteSql = "DELETE FROM book_list WHERE id = $deleteId";
if (mysqli_query($connect, $deleteSql)) {
header("Location: {$_SERVER['PHP_SELF']}");
exit();
} else {
echo "删除失败: " . mysqli_error($connect);
}
}
// 设置每页显示的图书数量(3行×3列=9本/页)
$booksPerPage = 9;
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($currentPage - 1) * $booksPerPage;
// 查询总记录数
$countSql = "SELECT COUNT(id) as total FROM book_list";
$countResult = mysqli_query($connect, $countSql);
$countRow = mysqli_fetch_assoc($countResult);
$totalBooks = $countRow['total'];
$totalPages = ceil($totalBooks / $booksPerPage);
// 查询当前页的图书数据
$sql = "SELECT id, book_name, img, author, brief, link FROM book_list ORDER BY id LIMIT $offset, $booksPerPage";
$result = mysqli_query($connect, $sql);
$books = [];
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$books[] = $row;
}
mysqli_free_result($result);
} else {
echo "数据库查询失败: " . mysqli_error($connect);
}
$defaultImage = "https://picsum.photos/400/600?random=book";
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图书管理系统 - 图书列表</title>
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
margin: 0;
padding: 20px;
background-color: #f8f9fa;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
margin-bottom: 40px;
}
.header h1 {
color: #333;
margin-bottom: 10px;
}
.pagination {
display: flex;
justify-content: center;
margin-top: 40px;
gap: 5px;
}
.pagination a, .pagination span {
padding: 8px 12px;
border-radius: 4px;
text-decoration: none;
background-color: white;
color: #337ab7;
border: 1px solid #ddd;
}
.pagination a:hover {
background-color: #f5f5f5;
}
.pagination .current {
background-color: #337ab7;
color: white;
border-color: #337ab7;
}
.book-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 30px;
}
.book-card {
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
overflow: hidden;
transition: transform 0.3s ease;
display: flex;
flex-direction: column;
position: relative;
cursor: pointer;
}
.book-card:hover {
transform: translateY(-5px);
}
.book-cover {
width: 100%;
height: 250px;
object-fit: cover;
user-select: none;
-webkit-user-drag: none;
-moz-user-select: none;
-ms-user-select: none;
}
.book-info {
padding: 20px;
flex-grow: 1;
display: flex;
flex-direction: column;
}
.book-title {
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
color: #222;
}
.book-author {
font-size: 16px;
color: #666;
margin-bottom: 15px;
}
.book-desc {
font-size: 14px;
color: #444;
line-height: 1.6;
flex-grow: 1;
}
.no-books {
text-align: center;
color: #666;
font-size: 18px;
padding: 40px;
grid-column: span 3;
}
.delete-btn {
background-color: #dc3545;
color: white;
border: none;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-top: 15px;
z-index: 2;
position: relative;
transition: background-color 0.3s;
}
.delete-btn:hover {
background-color: #c82333;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<p>共 <?= $totalBooks ?> 本图书</p>
</div>
<div class="book-grid">
<?php
if (!empty($books)) {
foreach ($books as $index => $book) {
$globalIndex = $offset + $index;
$imageUrl = $globalIndex < 6 && isset($firstSixImages[$globalIndex])
? ($firstSixImages[$globalIndex] ?: $defaultImage)
: ($book['img'] ?: $defaultImage);
?>
<div class="book-card" onclick="window.location.href='book_detail.php?id=<?= $book['id']; ?>'">
<img
src="<?= $imageUrl ?>"
alt="<?= htmlspecialchars($book['book_name']); ?>"
class="book-cover"
onerror="this.src='<?= $defaultImage ?>'; this.onerror=null;"
oncontextmenu="return false;"
draggable="false"
>
<div class="book-info">
<h2 class="book-title"><?= htmlspecialchars($book['book_name']); ?></h2>
<p class="book-author">作者:<?= htmlspecialchars($book['author'] ?? '未知'); ?></p>
<p class="book-desc"><?= htmlspecialchars($book['brief'] ?? '暂无简介'); ?></p>
<button
class="delete-btn"
onclick="if(confirm('确定要删除这本书吗?')) { window.location.href='?delete_id=<?= $book['id']; ?>'; event.stopPropagation(); }"
>
删除图书
</button>
</div>
</div>
<?php
}
} else {
?>
<div class="no-books">没有书籍信息</div>
<?php
}
?>
</div>
<div class="pagination">
<?php if ($currentPage > 1): ?>
<a href="?page=<?= $currentPage - 1 ?>">上一页</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<?php if ($i == $currentPage): ?>
<span class="current"><?= $i ?></span>
<?php else: ?>
<a href="?page=<?= $i ?>"><?= $i ?></a>
<?php endif; ?>
<?php endfor; ?>
<?php if ($currentPage < $totalPages): ?>
<a href="?page=<?= $currentPage + 1 ?>">下一页</a>
<?php endif; ?>
</div>
</div>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。