写一套完整的表白墙源码
<!DOCTYPE html>
<html>
<head>
<title>表白墙</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body{
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
header{
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
.container{
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
form{
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
input[type="text"], textarea{
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: none;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
}
input[type="submit"]{
background-color: #333;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.messages{
margin-top: 20px;
}
.message{
background-color: #fff;
padding: 20px;
margin-bottom: 20px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
}
.message p{
margin: 0;
font-size: 18px;
font-weight: bold;
}
.message span{
font-size: 14px;
color: #999;
}
</style>
</head>
<body>
<header>
<h1>表白墙</h1>
</header>
<div class="container">
<form action="submit.php" method="post">
<label for="name">姓名:</label>
<input type="text" name="name" id="name" required>
<label for="content">表白内容:</label>
<textarea name="content" id="content" rows="5" required></textarea>
<input type="submit" value="提交">
</form>
<div class="messages">
<?php
//连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
<pre><code> $conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
//从数据库中获取表白信息
$sql = "SELECT * FROM messages ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "<div class='message'><p>" . $row["name"]. "</p><p>" . $row["content"] . "</p><span>" . $row["date"]. "</span></div>";
}
} else {
echo "暂无表白信息";
}
$conn->close();
?>
</div>
</div>
</code></pre>
</body>
</html>
<!-- submit.php -->
<?php
//连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
//获取表单信息并插入数据库
$name = $_POST["name"];
$content = $_POST["content"];
$sql = "INSERT INTO messages (name, content)
VALUES ('$name', '$content')";
if ($conn->query($sql) === TRUE) {
echo "<script>alert('表白成功!');window.location.href='index.php';</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
原文地址: https://www.cveoy.top/t/topic/bC47 著作权归作者所有。请勿转载和采集!