用PHP+Mysql写一个论坛(包括用户账号的注册登录与修改密码以及能够发帖回复并删除帖子)的代码并给出表的创建代码 详细代码并说明php文件名称另:不使用pdo
时间: 2024-05-14 07:17:42 浏览: 144
php+mysql论坛程序
5星 · 资源好评率100%
很高兴为您提供这个代码示例。以下是一个简单的PHP MySQL论坛应用程序示例,它包括用户账户注册,登录,修改密码,发帖,回帖和删除帖子。这里我们使用原生MySQLi来连接MySQL,不使用PDO。
首先,我们需要创建数据库和表。您可以使用以下SQL语句来创建一个名为 `forum` 的数据库和三个表:`users`,`topics`和`posts`。
```sql
CREATE DATABASE forum;
USE forum;
CREATE TABLE users (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password CHAR(60) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);
CREATE TABLE topics (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
subject VARCHAR(255) NOT NULL,
user_id INT(11) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE posts (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
content TEXT NOT NULL,
topic_id INT(11) NOT NULL,
user_id INT(11) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (topic_id) REFERENCES topics(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
```
接下来,让我们逐步解释PHP代码。首先,我们定义了一些常量,包括数据库连接信息、帖子每页显示的数量等。然后我们连接到MySQL服务器并选择 `forum` 数据库:
```php
<?php
// 定义数据库连接信息
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'forum');
define('POSTS_PER_PAGE', 10);
// 连接到 MySQL 服务器
$conn = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
// 选择数据库
mysqli_select_db($conn, DB_NAME);
```
接下来,我们定义了一些函数来处理用户登录、注册和修改密码。我们使用 `mysqli_real_escape_string()` 函数来防止SQL注入攻击。
```php
// 用户注册
function register_user($username, $password, $email) {
global $conn;
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$hashed_password', '$email')";
mysqli_query($conn, $sql);
}
// 用户登录
function login_user($username, $password) {
global $conn;
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
$user = mysqli_fetch_assoc($result);
if (password_verify($password, $user['password'])) {
return $user;
} else {
return false;
}
} else {
return false;
}
}
// 修改密码
function change_password($user_id, $new_password) {
global $conn;
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$sql = "UPDATE users SET password='$hashed_password' WHERE id=$user_id";
mysqli_query($conn, $sql);
}
```
然后,我们定义了一些函数来处理主题和帖子。`get_topics()`函数返回所有主题的列表,`create_topic()`函数创建一个新主题,`get_posts()`函数返回指定主题的所有帖子,`create_post()`函数创建一个新帖子,`delete_post()`函数删除指定的帖子。
```php
// 获取所有主题
function get_topics() {
global $conn;
$sql = "SELECT topics.*, users.username FROM topics JOIN users ON topics.user_id=users.id ORDER BY created_at DESC";
$result = mysqli_query($conn, $sql);
$topics = mysqli_fetch_all($result, MYSQLI_ASSOC);
return $topics;
}
// 创建新主题
function create_topic($subject, $user_id) {
global $conn;
$subject = mysqli_real_escape_string($conn, $subject);
$sql = "INSERT INTO topics (subject, user_id) VALUES ('$subject', $user_id)";
mysqli_query($conn, $sql);
}
// 获取所有帖子
function get_posts($topic_id, $page = 1) {
global $conn;
$offset = ($page - 1) * POSTS_PER_PAGE;
$sql = "SELECT posts.*, users.username FROM posts JOIN users ON posts.user_id=users.id WHERE topic_id=$topic_id ORDER BY created_at LIMIT $offset, " . POSTS_PER_PAGE;
$result = mysqli_query($conn, $sql);
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
return $posts;
}
// 创建新帖子
function create_post($content, $topic_id, $user_id) {
global $conn;
$content = mysqli_real_escape_string($conn, $content);
$sql = "INSERT INTO posts (content, topic_id, user_id) VALUES ('$content', $topic_id, $user_id)";
mysqli_query($conn, $sql);
}
// 删除帖子
function delete_post($post_id) {
global $conn;
$sql = "DELETE FROM posts WHERE id=$post_id";
mysqli_query($conn, $sql);
}
```
最后,我们定义了一些路由来处理用户请求。在此示例中,我们只使用了一种路由:`index.php`。它显示所有主题或指定主题的所有帖子,允许用户登录,注销,注册,创建新主题,创建新帖子和删除帖子。
```php
<?php
session_start();
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'forum');
define('POSTS_PER_PAGE', 10);
$conn = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
mysqli_select_db($conn, DB_NAME);
if (isset($_GET['action'])) {
if ($_GET['action'] == 'logout') {
unset($_SESSION['user_id']);
header('Location: index.php');
exit;
} else if ($_GET['action'] == 'delete_post' && isset($_GET['post_id'])) {
delete_post($_GET['post_id']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
}
if (isset($_POST['action'])) {
if ($_POST['action'] == 'login') {
$user = login_user($_POST['username'], $_POST['password']);
if ($user) {
$_SESSION['user_id'] = $user['id'];
header('Location: index.php');
exit;
} else {
$error = 'Invalid username or password.';
}
} else if ($_POST['action'] == 'register') {
register_user($_POST['username'], $_POST['password'], $_POST['email']);
$success = 'Your account has been created successfully. Please log in.';
} else if ($_POST['action'] == 'create_topic') {
create_topic($_POST['subject'], $_SESSION['user_id']);
header('Location: index.php');
exit;
} else if ($_POST['action'] == 'create_post') {
create_post($_POST['content'], $_POST['topic_id'], $_SESSION['user_id']);
header('Location: index.php?topic_id=' . $_POST['topic_id']);
exit;
} else if ($_POST['action'] == 'change_password') {
change_password($_SESSION['user_id'], $_POST['new_password']);
$success = 'Your password has been changed successfully.';
}
}
if (isset($_GET['topic_id'])) {
$topic_id = $_GET['topic_id'];
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$topic = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM topics WHERE id=$topic_id"));
$posts = get_posts($topic_id, $page);
$total_posts = mysqli_fetch_row(mysqli_query($conn, "SELECT COUNT(*) FROM posts WHERE topic_id=$topic_id"))[0];
$total_pages = ceil($total_posts / POSTS_PER_PAGE);
include 'views/topic.php';
} else {
$topics = get_topics();
include 'views/index.php';
}
```
最后,我们定义了两个视图文件: `views/index.php` 和 `views/topic.php`。`index.php` 显示所有主题的列表,以及一个表单允许用户登录、注册和创建新主题。`topic.php` 显示指定主题的所有帖子,以及允许用户创建新帖子和删除帖子的表单。
这里只给出了主要的PHP代码示例,您可以将其放在 `index.php` 文件中,然后在和它相同的目录下创建 `views/index.php` 和 `views/topic.php` 文件。
阅读全文