PHP大作业我的博客及详细代码
时间: 2023-10-21 07:26:07 浏览: 34
很棒!下面是一个简单的博客系统的示例代码:
1. 创建数据库表格
首先,我们需要创建一个MySQL数据库,并创建两个表格:`users`和`posts`。
```
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`author_id` int(11) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `author_id` (`author_id`),
CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 创建PHP连接数据库的类
接下来,我们需要编写一个PHP类,用于连接数据库和执行一些常见的数据库操作。这里我们使用PDO来连接MySQL数据库。
```php
class Database {
private $host = 'localhost';
private $username = 'root';
private $password = 'password';
private $database = 'blog';
private $connection;
public function __construct() {
try {
$dsn = "mysql:host={$this->host};dbname={$this->database}";
$this->connection = new PDO($dsn, $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
}
public function query($sql) {
$stmt = $this->connection->prepare($sql);
$stmt->execute();
return $stmt;
}
public function lastInsertId() {
return $this->connection->lastInsertId();
}
}
```
3. 用户登录和注册功能
接下来,我们需要编写用户登录和注册的功能。这里我们使用session来保存用户登录状态。
```php
session_start();
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username = :username";
$stmt = $db->query($sql);
$stmt->bindParam(':username', $username);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
header('Location: index.php');
exit;
} else {
$error = 'Invalid username or password';
}
}
if (isset($_POST['register'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$sql = "INSERT INTO users (username, password, email) VALUES (:username, :password, :email)";
$stmt = $db->query($sql);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', password_hash($password, PASSWORD_DEFAULT));
$stmt->bindParam(':email', $email);
$stmt->execute();
$_SESSION['user_id'] = $db->lastInsertId();
header('Location: index.php');
exit;
}
```
4. 添加博客文章
现在,我们可以添加博客文章了。我们需要一个表单,让用户输入文章标题和内容。当用户提交表单时,我们将文章信息插入到`posts`表中。
```php
if (isset($_POST['create_post'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$author_id = $_SESSION['user_id'];
$created_at = date('Y-m-d H:i:s');
$sql = "INSERT INTO posts (title, content, author_id, created_at) VALUES (:title, :content, :author_id, :created_at)";
$stmt = $db->query($sql);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->bindParam(':author_id', $author_id);
$stmt->bindParam(':created_at', $created_at);
$stmt->execute();
header('Location: index.php');
exit;
}
```
5. 显示博客文章
最后,我们需要显示博客文章。我们从`posts`表中获取所有文章,并将它们显示在网页上。
```php
$sql = "SELECT * FROM posts ORDER BY created_at DESC";
$stmt = $db->query($sql);
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($posts as $post) {
$sql = "SELECT * FROM users WHERE id = :id";
$stmt = $db->query($sql);
$stmt->bindParam(':id', $post['author_id']);
$stmt->execute();
$author = $stmt->fetch(PDO::FETCH_ASSOC);
echo '<h1>' . $post['title'] . '</h1>';
echo '<p>' . $post['content'] . '</p>';
echo '<small>Written by ' . $author['username'] . ' on ' . $post['created_at'] . '</small>';
}
```
这就是一个简单的博客系统的示例代码。当然,它仅仅是一个示例,实际上你需要根据你的需求和想法进行修改和拓展。希望对你有所帮助!