<?php
// 简单留言板单页php源码
// 连接数据库
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die(“连接失败: ” . $conn->connect_error);
}
// 添加留言
if(isset($_POST[‘submit’])){
$message = $_POST[‘message’];
$emoji = $_POST[’emoji’];
$sql = “INSERT INTO messages (message, emoji) VALUES (‘$message’, ‘$emoji’)”;
if ($conn->query($sql) === TRUE) {
echo “留言添加成功”;
} else {
echo “Error: ” . $sql . “<br>” . $conn->error;
}
}
// 获取留言
$sql = “SELECT * FROM messages”;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo “留言: ” . $row[“message”]. ” – 表情: ” . $row[“emoji”]. “<br>”;
}
} else {
echo “0 个结果”;
}
$conn->close();
?>
// 问题: 留言刷新页面后消失,需要加入储存功能。
评论(0)