用PHP和MySQL语言实现论坛(功能包括用户登录和注册,更改密码。发表帖子,回复帖子和删除帖子)给出完整代码并说明每个PHP代码名称和表创建代码
时间: 2023-06-12 14:07:21 浏览: 158
首先,我们需要创建一个数据库和相应的表来存储用户信息、帖子和回复等数据。我们可以使用以下 SQL 代码来创建相应的表:
```
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE posts (
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE replies (
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) NOT NULL,
post_id INT(11) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE
);
```
接下来,我们可以编写 PHP 代码实现论坛功能。以下是每个 PHP 文件的作用和代码示例:
- config.php:数据库连接配置文件。
```php
<?php
// 数据库连接信息
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'forum';
// 创建数据库连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
?>
```
- register.php:用户注册页面。
```php
<?php
require_once 'config.php';
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
// 检查用户名是否已存在
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$error = 'Username already exists';
} else {
// 插入新用户记录
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
header('Location: login.php');
} else {
$error = 'Registration failed';
}
}
}
// 渲染注册页面
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<?php if (isset($error)): ?>
<p><?php echo $error; ?></p>
<?php endif; ?>
<form method="post">
<label>Username:</label>
<input type="text" name="username" required>
<br>
<label>Password:</label>
<input type="password" name="password" required>
<br>
<button type="submit">Register</button>
</form>
</body>
</html>
```
- login.php:用户登录页面。
```php
<?php
require_once 'config.php';
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
// 检查用户名和密码是否匹配
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_assoc($result);
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header('Location: index.php');
} else {
$error = 'Invalid username or password';
}
}
// 渲染登录页面
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<?php if (isset($error)): ?>
<p><?php echo $error; ?></p>
<?php endif; ?>
<form method="post">
<label>Username:</label>
<input type="text" name="username" required>
<br>
<label>Password:</label>
<input type="password" name="password" required>
<br>
<button type="submit">Login</button>
</form>
</body>
</html>
```
- logout.php:用户退出登录。
```php
<?php
session_start();
session_destroy();
header('Location: login.php');
?>
```
- change_password.php:修改密码页面。
```php
<?php
require_once 'config.php';
// 检查用户是否已登录
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_SESSION['user_id'];
$password = $_POST['password'];
// 更新用户密码
$sql = "UPDATE users SET password='$password' WHERE id=$user_id";
if (mysqli_query($conn, $sql)) {
header('Location: index.php');
} else {
$error = 'Password change failed';
}
}
// 渲染修改密码页面
?>
<!DOCTYPE html>
<html>
<head>
<title>Change Password</title>
</head>
<body>
<h1>Change Password</h1>
<?php if (isset($error)): ?>
<p><?php echo $error; ?></p>
<?php endif; ?>
<form method="post">
<label>New Password:</label>
<input type="password" name="password" required>
<br>
<button type="submit">Change Password</button>
</form>
</body>
</html>
```
- index.php:论坛首页,显示所有帖子。
```php
<?php
require_once 'config.php';
// 查询所有帖子
$sql = "SELECT posts.*, users.username FROM posts INNER JOIN users ON posts.user_id=users.id ORDER BY posts.created_at DESC";
$result = mysqli_query($conn, $sql);
// 渲染帖子列表页面
?>
<!DOCTYPE html>
<html>
<head>
<title>Forum</title>
</head>
<body>
<h1>Forum</h1>
<p>Welcome, <?php echo $_SESSION['username']; ?>!</p>
<a href="new_post.php">New Post</a>
<a href="change_password.php">Change Password</a>
<a href="logout.php">Logout</a>
<hr>
<?php while ($post = mysqli_fetch_assoc($result)): ?>
<h2><?php echo $post['title']; ?></h2>
<p><?php echo $post['content']; ?></p>
<p>By <?php echo $post['username']; ?> on <?php echo $post['created_at']; ?></p>
<a href="delete_post.php?id=<?php echo $post['id']; ?>">Delete</a>
<hr>
<?php
// 查询该帖子的所有回复
$sql = "SELECT replies.*, users.username FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.post_id={$post['id']} ORDER BY replies.created_at ASC";
$result2 = mysqli_query($conn, $sql);
while ($reply = mysqli_fetch_assoc($result2)):
?>
<p><?php echo $reply['content']; ?></p>
<p>By <?php echo $reply['username']; ?> on <?php echo $reply['created_at']; ?></p>
<a href="delete_reply.php?id=<?php echo $reply['id']; ?>">Delete</a>
<hr>
<?php endwhile; ?>
<form method="post" action="new_reply.php">
<input type="hidden" name="post_id" value="<?php echo $post['id']; ?>">
<label>New Reply:</label>
<textarea name="content" required></textarea>
<br>
<button type="submit">Reply</button>
</form>
<hr>
<?php endwhile; ?>
</body>
</html>
```
- new_post.php:发表新帖子页面。
```php
<?php
require_once 'config.php';
// 检查用户是否已登录
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_SESSION['user_id'];
$title = $_POST['title'];
$content = $_POST['content'];
// 插入新帖子记录
$sql = "INSERT INTO posts (user_id, title, content) VALUES ($user_id, '$title', '$content')";
if (mysqli_query($conn, $sql)) {
header('Location: index.php');
} else {
$error = 'Post creation failed';
}
}
// 渲染发表新帖子页面
?>
<!DOCTYPE html>
<html>
<head>
<title>New Post</title>
</head>
<body>
<h1>New Post</h1>
<?php if (isset($error)): ?>
<p><?php echo $error; ?></p>
<?php endif; ?>
<form method="post">
<label>Title:</label>
<input type="text" name="title" required>
<br>
<label>Content:</label>
<textarea name="content" required></textarea>
<br>
<button type="submit">Create Post</button>
</form>
</body>
</html>
```
- delete_post.php:删除帖子功能。
```php
<?php
require_once 'config.php';
// 检查用户是否已登录
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
}
// 获取要删除的帖子 ID
$post_id = $_GET['id'];
// 删除帖子及其所有回复
$sql = "DELETE FROM posts WHERE id=$post_id";
mysqli_query($conn, $sql);
$sql = "DELETE FROM replies WHERE post_id=$post_id";
mysqli_query($conn, $sql);
// 返回论坛首页
header('Location: index.php');
?>
```
- new_reply.php:发表新回复功能。
```php
<?php
require_once 'config.php';
// 检查用户是否已登录
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_SESSION['user_id'];
$post_id = $_POST['post_id'];
$content = $_POST['content'];
// 插入新回复记录
$sql = "INSERT INTO replies (user_id, post_id, content) VALUES ($user_id, $post_id, '$content')";
if (mysqli_query($conn, $sql)) {
header('Location: index.php');
} else {
$error = 'Reply creation failed';
}
}
// 渲染发表新回复页面
?>
<!DOCTYPE html>
<html>
<head>
<title>New Reply</title>
</head>
<body>
<h1>New Reply</h1>
<?php if (isset($error)): ?>
<p><?php echo $error; ?></p>
<?php endif; ?>
<form method="post">
<input type="hidden" name="post_id" value="<?php echo $_POST['post_id']; ?>">
<label>Reply:</label>
<textarea name="content" required></textarea>
<br>
<button type="submit">Reply</button>
</form>
</body>
</html>
```
- delete_reply.php:删除回复功能。
```php
<?php
require_once 'config.php';
// 检查用户是否已登录
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
}
// 获取要删除的回复 ID
$reply_id = $_GET['id'];
// 删除回复记录
$sql = "DELETE FROM replies WHERE id=$reply_id";
mysqli_query($conn, $sql);
// 返回论坛首页
header('Location: index.php');
?>
```
阅读全文