以下是一个简单的PHP相册管理系统的代码示例:
CREATE TABLE albums (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE photos ( id INT AUTO_INCREMENT PRIMARY KEY, album_id INT NOT NULL, name VARCHAR(100) NOT NULL, description TEXT, file_name VARCHAR(100) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (album_id) REFERENCES albums(id) ON DELETE CASCADE );
2. 创建相册管理页面(index.php):
```php
<?php
// 数据库连接配置
$host = 'localhost';
$db = 'your_database';
$user = 'your_username';
$password = 'your_password';
// 连接数据库
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $password);
// 获取相册列表
$albums = $conn->query("SELECT * FROM albums")->fetchAll(PDO::FETCH_ASSOC);
// 添加新相册
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$stmt = $conn->prepare("INSERT INTO albums (name) VALUES (:name)");
$stmt->bindParam(':name', $name);
$stmt->execute();
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>相册管理系统</title>
</head>
<body>
<h1>相册管理系统</h1>
<h2>相册列表:</h2>
<ul>
<?php foreach ($albums as $album): ?>
<li>
<a href="album.php?id=<?php echo $album['id']; ?>">
<?php echo $album['name']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<h2>添加新相册:</h2>
<form method="POST" action="">
<input type="text" name="name" placeholder="相册名称" required>
<button type="submit">添加</button>
</form>
</body>
</html>
<?php
// 数据库连接配置
$host = 'localhost';
$db = 'your_database';
$user = 'your_username';
$password = 'your_password';
// 连接数据库 $conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $password);
// 获取相册信息 $albumId = $_GET['id']; $album = $conn->prepare("SELECT * FROM albums WHERE id = :id"); $album->bindParam(':id', $albumId); $album->execute(); $album = $album->fetch(PDO::FETCH_ASSOC);
// 获取相册中的照片列表 $photos = $conn->prepare("SELECT * FROM photos WHERE album_id = :album_id"); $photos->bindParam(':album_id', $albumId); $photos->execute(); $photos = $photos->fetchAll(PDO::FETCH_ASSOC);
// 上传新照片 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = $_FILES['photo']; $name = $_POST['name']; $description = $POST['description']; $fileName = time() . '' . $file['name']; move_uploaded_file($file['tmp_name'], 'uploads/' . $fileName); $stmt = $conn->prepare("INSERT INTO photos (album_id, name, description, file_name) VALUES (:album_id, :name, :description, :file_name)"); $stmt->bindParam(':album_id', $albumId); $stmt->bindParam(':name', $name); $stmt->bindParam(':description', $description); $stmt->bindParam(':file_name', $fileName); $stmt->execute(); header("Location: album.php?id=$albumId"); exit(); } ?>
<!DOCTYPE html>
请注意,这只是一个简单的示例,还可以根据需要进行更多的功能和安全性改进。
上一篇:php 只获取文件名(php提供上传文件的实际名称)
下一篇:dom php xml
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站