car-website/
├── index.php # 首页(汽车列表)
├── car-detail.php # 汽车详情页
├── search.php # 搜索功能
├── add-car.php # 添加汽车(后台)
├── config.php # 数据库配置
├── style.css # 样式文件
└── database.sql # 数据库结构
CREATE TABLE cars (
id INT AUTO_INCREMENT PRIMARY KEY,
make VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
year INT NOT NULL,
price DECIMAL(10,2) NOT NULL,
image VARCHAR(100) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'car_database');
try {
$pdo = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
?>
<?php
require_once 'config.php';
// 分页逻辑
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 6;
$offset = ($page 1) * $perPage;
// 获取汽车数据
$stmt = $pdo->prepare("SELECT * FROM cars LIMIT :offset, :perPage");
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':perPage', $perPage, PDO::PARAM_INT);
$stmt->execute();
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 获取总数用于分页
$totalCars = $pdo->query("SELECT COUNT(*) FROM cars")->fetchColumn();
$totalPages = ceil($totalCars / $perPage);
?>
<!DOCTYPE html>
<html>
<head>汽车销售网站</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>汽车列表</h1>
<div class="car-grid">
<?php foreach ($cars as $car): ?>
<div class="car-card">
<img src="images/<?php echo $car['image']; ?>" alt="<?php echo $car['make']; ?>">
<h3><?php echo $car['make'] . ' ' . $car['model']; ?></h3>
<p>年份: <?php echo $car['year']; ?></p>
<p>价格: $<?php echo number_format($car['price'], 2); ?></p>
<a href="car-detail.php?id=<?php echo $car['id']; ?>">查看详情</a>
</div>
<?php endforeach; ?>
</div>
<!-分页导航 -->
<div class="pagination">
<?php if ($page > 1): ?>
<a href="?page=<?php echo $page 1; ?>">上一页</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<a href="?page=<?php echo $i; ?>" <?php echo $i == $page ? 'class="active"' : ''; ?>>
<?php echo $i; ?>
</a>
<?php endfor; ?>
<?php if ($page < $totalPages): ?>
<a href="?page=<?php echo $page + 1; ?>">下一页</a>
<?php endif; ?>
</div>
</body>
</html>
<?php
require_once 'config.php';
if (!isset($_GET['id'])) {
die("缺少汽车ID");
}
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM cars WHERE id = ?");
$stmt->execute([$id]);
$car = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$car) {
die("汽车不存在");
}
?>
<!DOCTYPE html>
<html>
<head><?php echo $car['make'] . ' ' . $car['model']; ?></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="car-detail">
<img src="images/<?php echo $car['image']; ?>" alt="<?php echo $car['make']; ?>">
<div class="info">
<h1><?php echo $car['make'] . ' ' . $car['model']; ?></h1>
<p>年份: <?php echo $car['year']; ?></p>
<p>价格: $<?php echo number_format($car['price'], 2); ?></p>
<p><?php echo nl2br($car['description']); ?></p>
</div>
</div>
</body>
</html>
<?php
require_once 'config.php';
$searchTerm = isset($_GET['search']) ? trim($_GET['search']) : '';
$cars = [];
if (!empty($searchTerm)) {
$stmt = $pdo->prepare("SELECT * FROM cars WHERE make LIKE ? OR model LIKE ?");
$searchTerm = "%$searchTerm%";
$stmt->execute([$searchTerm, $searchTerm]);
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<head>搜索结果</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>搜索结果</h1>
<form method="get" action="search.php">
<input type="text" name="search" value="<?php echo htmlspecialchars($searchTerm); ?>" placeholder="搜索品牌或型号">
<button type="submit">搜索</button>
</form>
<div class="car-grid">
<?php foreach ($cars as $car): ?>
<div class="car-card">
<img src="images/<?php echo $car['image']; ?>" alt="<?php echo $car['make']; ?>">
<h3><?php echo $car['make'] . ' ' . $car['model']; ?></h3>
<p>价格: $<?php echo number_format($car['price'], 2); ?></p>
<a href="car-detail.php?id=<?php echo $car['id']; ?>">查看详情</a>
</div>
<?php endforeach; ?>
</div>
</body>
</html>
<?php
require_once 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$make = $_POST['make'];
$model = $_POST['model'];
$year = $_POST['year'];
$price = $_POST['price'];
$description = $_POST['description'];
$image = 'default.jpg'; // 实际中需处理文件上传
$stmt = $pdo->prepare("INSERT INTO cars (make, model, year, price, description, image) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$make, $model, $year, $price, $description, $image]);
header('Location: index.php');
exit();
}
?>
<!DOCTYPE html>
<html>
<head>添加汽车</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>添加新汽车</h1>
<form method="post">
<label>品牌: <input type="text" name="make" required></label>
<label>型号: <input type="text" name="model" required></label>
<label>年份: <input type="number" name="year" required></label>
<label>价格: <input type="number" step="0.01" name="price" required></label>
<label>描述: <textarea name="description" required></textarea></label>
<button type="submit">提交</button>
</form>
</body>
</html>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.car-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
margin-top: 20px;
}
.car-card {
background: white;
border: 1px solid #ddd;
padding: 15px;
text-align: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.car-card img {
max-width: 100%;
height: 200px;
object-fit: cover;
}
.pagination {
margin: 20px 0;
text-align: center;
}
.pagination a {
margin: 0 5px;
padding: 5px 10px;
border: 1px solid #ddd;
text-decoration: none;
}
.pagination a.active {
background: #007bff;
color: white;
}
database.sql创建表结构。images文件夹并上传汽车图片。htmlspecialchars)⚠️ 注意:此代码仅作学习参考,商业使用需完善安全性和功能细节。


