web项目实训利用html做书籍信息系统的详细代码
时间: 2023-08-13 10:04:32 浏览: 86
图书网站html5代码.docx
5星 · 资源好评率100%
由于书籍信息系统的代码比较长,无法在这里完整展示,我给你提供一些关键点和示例代码,供你参考。
1. HTML 页面布局和样式
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>书籍信息系统</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: #333;
color: #fff;
padding: 10px;
text-align: center;
}
nav {
background-color: #eee;
padding: 10px;
text-align: center;
}
nav a {
display: inline-block;
margin: 0 10px;
text-decoration: none;
color: #333;
}
nav a:hover {
color: #fff;
background-color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.form-group {
margin-bottom: 10px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
.form-group input:focus {
border-color: #333;
}
.btn {
background-color: #333;
color: #fff;
padding: 5px 10px;
border: none;
border-radius: 3px;
cursor: pointer;
}
.btn:hover {
background-color: #666;
}
.search-form {
margin-bottom: 20px;
}
.search-form input {
width: 80%;
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
.search-form button {
background-color: #333;
color: #fff;
padding: 5px 10px;
border: none;
border-radius: 3px;
cursor: pointer;
}
.search-form button:hover {
background-color: #666;
}
.book-list {
list-style: none;
padding: 0;
margin: 0;
}
.book-list li {
padding: 10px;
margin-bottom: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
.book-list li:hover {
background-color: #eee;
}
</style>
</head>
<body>
<header class="header">
<h1>书籍信息系统</h1>
</header>
<nav>
<a href="#">首页</a>
<a href="#">添加书籍</a>
<a href="#">查找书籍</a>
</nav>
<div class="container">
<h2>添加书籍</h2>
<form action="add_book.php" method="post">
<div class="form-group">
<label for="book-name">书籍名称:</label>
<input type="text" id="book-name" name="book_name" required>
</div>
<div class="form-group">
<label for="author">作者:</label>
<input type="text" id="author" name="author" required>
</div>
<div class="form-group">
<label for="publisher">出版社:</label>
<input type="text" id="publisher" name="publisher" required>
</div>
<div class="form-group">
<label for="published-date">出版日期:</label>
<input type="date" id="published-date" name="published_date" required>
</div>
<button type="submit" class="btn">添加</button>
</form>
<hr>
<h2>查找书籍</h2>
<form class="search-form" action="search_book.php" method="get">
<input type="text" id="keywords" name="keywords" placeholder="请输入关键词">
<button type="submit" class="btn">查找</button>
</form>
<ul class="book-list">
<li>
<h3>《JavaScript高级程序设计》</h3>
<p>作者:Nicholas C. Zakas</p>
<p>出版社:人民邮电出版社</p>
<p>出版日期:2013-03-01</p>
</li>
<li>
<h3>《深入浅出Node.js》</h3>
<p>作者:朴灵</p>
<p>出版社:人民邮电出版社</p>
<p>出版日期:2013-12-01</p>
</li>
<li>
<h3>《CSS揭秘》</h3>
<p>作者:Lea Verou</p>
<p>出版社:人民邮电出版社</p>
<p>出版日期:2015-08-01</p>
</li>
</ul>
</div>
</body>
</html>
```
2. JavaScript 表单验证
```html
<script>
function validateForm() {
var bookName = document.getElementById("book-name").value;
var author = document.getElementById("author").value;
var publisher = document.getElementById("publisher").value;
var publishedDate = document.getElementById("published-date").value;
if (bookName == "") {
alert("请输入书籍名称!");
return false;
}
if (author == "") {
alert("请输入作者名称!");
return false;
}
if (publisher == "") {
alert("请输入出版社名称!");
return false;
}
if (publishedDate == "") {
alert("请输入出版日期!");
return false;
}
return true;
}
</script>
```
3. PHP 后台程序
add_book.php
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "password", "book_info");
// 检测连接
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取表单数据
$bookName = $_POST["book_name"];
$author = $_POST["author"];
$publisher = $_POST["publisher"];
$publishedDate = $_POST["published_date"];
// 构造 SQL 语句
$sql = "INSERT INTO books (book_name, author, publisher, published_date) VALUES ('$bookName', '$author', '$publisher', '$publishedDate')";
if (mysqli_query($conn, $sql)) {
echo "添加成功!";
} else {
echo "添加失败:" . mysqli_error($conn);
}
// 关闭连接
mysqli_close($conn);
?>
```
search_book.php
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "password", "book_info");
// 检测连接
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取关键词
$keywords = $_GET["keywords"];
// 构造 SQL 语句
$sql = "SELECT * FROM books WHERE book_name LIKE '%$keywords%' OR author LIKE '%$keywords%' OR publisher LIKE '%$keywords%' OR published_date LIKE '%$keywords%'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "<li>";
echo "<h3>" . $row["book_name"] . "</h3>";
echo "<p>作者:" . $row["author"] . "</p>";
echo "<p>出版社:" . $row["publisher"] . "</p>";
echo "<p>出版日期:" . $row["published_date"] . "</p>";
echo "</li>";
}
} else {
echo "没有找到相关书籍!";
}
// 关闭连接
mysqli_close($conn);
?>
```
以上是书籍信息系统的关键代码,你可以根据需求进行调整和完善。
阅读全文