<!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-&gt;connect_error) {
			    die(&quot;连接失败: &quot; . $conn-&gt;connect_error);
			}

			//从数据库中获取表白信息
			$sql = &quot;SELECT * FROM messages ORDER BY id DESC&quot;;
			$result = $conn-&gt;query($sql);

			if ($result-&gt;num_rows &gt; 0) {
			    // 输出数据
			    while($row = $result-&gt;fetch_assoc()) {
			        echo &quot;&lt;div class='message'&gt;&lt;p&gt;&quot; . $row[&quot;name&quot;]. &quot;&lt;/p&gt;&lt;p&gt;&quot; . $row[&quot;content&quot;] . &quot;&lt;/p&gt;&lt;span&gt;&quot; . $row[&quot;date&quot;]. &quot;&lt;/span&gt;&lt;/div&gt;&quot;;
			    }
			} else {
			    echo &quot;暂无表白信息&quot;;
			}
			$conn-&gt;close();
		?&gt;
	&lt;/div&gt;
&lt;/div&gt;
</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 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录