php实现表白墙
实现表白墙的PHP代码
以下是一个简单的PHP表白墙实现方案,包含前端表单提交和后端数据存储功能。
数据库表结构
CREATE TABLE `love_wall` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sender` varchar(50) NOT NULL,
`receiver` varchar(50) NOT NULL,
`message` text NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
表单提交页面 (submit.php)

<!DOCTYPE html>
<html>
<head>
<title>表白墙</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
form { display: grid; gap: 15px; }
input, textarea { padding: 10px; border: 1px solid #ddd; }
button { padding: 10px; background: #ff6b81; color: white; border: none; cursor: pointer; }
</style>
</head>
<body>
<h1>表白墙</h1>
<form action="save.php" method="post">
<input type="text" name="sender" placeholder="你的名字" required>
<input type="text" name="receiver" placeholder="TA的名字" required>
<textarea name="message" rows="5" placeholder="想对TA说的话..." required></textarea>
<button type="submit">提交表白</button>
</form>
<p><a href="wall.php">查看表白墙</a></p>
</body>
</html>
数据保存处理 (save.php)
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$db = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
$sender = htmlspecialchars(trim($_POST['sender']));
$receiver = htmlspecialchars(trim($_POST['receiver']));
$message = htmlspecialchars(trim($_POST['message']));
$stmt = $db->prepare("INSERT INTO love_wall (sender, receiver, message) VALUES (?, ?, ?)");
$stmt->execute([$sender, $receiver, $message]);
header('Location: wall.php');
exit;
}
?>
表白墙展示页面 (wall.php)

<!DOCTYPE html>
<html>
<head>
<title>表白墙展示</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
.message { border: 1px solid #eee; padding: 15px; margin-bottom: 15px; border-radius: 5px; }
.sender { color: #ff6b81; font-weight: bold; }
.receiver { color: #1e90ff; font-weight: bold; }
.time { color: #999; font-size: 0.8em; }
</style>
</head>
<body>
<h1>表白墙</h1>
<p><a href="submit.php">我要表白</a></p>
<?php
$db = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
$messages = $db->query("SELECT * FROM love_wall ORDER BY created_at DESC")->fetchAll();
foreach ($messages as $msg) {
echo '<div class="message">';
echo '<p><span class="sender">'.$msg['sender'].'</span> 对 <span class="receiver">'.$msg['receiver'].'</span> 说:</p>';
echo '<p>'.$msg['message'].'</p>';
echo '<p class="time">'.date('Y-m-d H:i', strtotime($msg['created_at'])).'</p>';
echo '</div>';
}
?>
</body>
</html>
功能扩展建议
分页功能
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;
$total = $db->query("SELECT COUNT(*) FROM love_wall")->fetchColumn();
$totalPages = ceil($total / $perPage);
$messages = $db->query("SELECT * FROM love_wall ORDER BY created_at DESC LIMIT $offset, $perPage")->fetchAll();
匿名功能 在表单中添加匿名选项字段:
<input type="checkbox" name="anonymous" value="1"> 匿名发送
处理时:
$sender = $_POST['anonymous'] ? '匿名' : htmlspecialchars(trim($_POST['sender']));
安全注意事项
- 使用预处理语句防止SQL注入
- 对用户输入进行htmlspecialchars转义防止XSS攻击
- 限制输入长度防止数据库溢出
- 考虑添加验证码防止垃圾提交
部署说明
- 创建MySQL数据库并导入表结构
- 修改数据库连接信息
- 设置适当的文件权限
- 考虑添加.htaccess保护配置文件
这个实现包含了表白墙的基本功能,可以根据需要进一步扩展如添加点赞、评论等功能。






