<!DOCTYPE html>
<html>
<head>
	<title>留言板</title>
	<meta charset="utf-8">
	<style type="text/css">
		body {
			font-family: Arial, sans-serif;
			background-color: #f0f0f0;
		}
		h1 {
			text-align: center;
			margin-top: 50px;
		}
		form {
			width: 50%;
			margin: 0 auto;
			background-color: #fff;
			padding: 20px;
			border-radius: 10px;
			box-shadow: 0 0 10px #ccc;
		}
		label {
			display: block;
			margin-bottom: 10px;
			font-weight: bold;
		}
		input[type="text"], textarea {
			width: 100%;
			padding: 5px;
			border: 1px solid #ccc;
			border-radius: 5px;
			margin-bottom: 20px;
		}
		input[type="submit"] {
			background-color: #4CAF50;
			color: #fff;
			padding: 10px 20px;
			border: none;
			border-radius: 5px;
			cursor: pointer;
		}
		ul {
			list-style: none;
			padding: 0;
			margin-top: 50px;
		}
		li {
			background-color: #fff;
			box-shadow: 0 0 10px #ccc;
			border-radius: 10px;
			padding: 20px;
			margin-bottom: 20px;
		}
		li h3 {
			margin-top: 0;
		}
		li p {
			margin-bottom: 0;
		}
	</style>
</head>
<body>
	<h1>留言板</h1>
	<form method="post" action="submit.php">
		<label for="name">姓名:</label>
		<input type="text" name="name" id="name" required>
		<label for="email">邮箱:</label>
		<input type="text" name="email" id="email" required>
		<label for="message">留言:</label>
		<textarea name="message" id="message" rows="5" required></textarea>
		<input type="submit" value="提交">
	</form>
<pre><code>&lt;ul&gt;
	&lt;?php
	// 连接数据库
	$con = mysqli_connect('localhost', 'root', 'password', 'test');
	// 查询留言
	$result = mysqli_query($con, &quot;SELECT * FROM messages ORDER BY id DESC&quot;);
	// 遍历留言
	while ($row = mysqli_fetch_array($result)) {
		echo '&lt;li&gt;';
		echo '&lt;h3&gt;' . $row['name'] . ' &lt;small&gt;' . $row['email'] . '&lt;/small&gt;&lt;/h3&gt;';
		echo '&lt;p&gt;' . $row['message'] . '&lt;/p&gt;';
		echo '&lt;/li&gt;';
	}
	// 关闭连接
	mysqli_close($con);
	?&gt;
&lt;/ul&gt;
</code></pre>
</body>
</html>
<?php
// 处理提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	// 连接数据库
	$con = mysqli_connect('localhost', 'root', 'password', 'test');
	// 构造 SQL 插入语句
	$sql = "INSERT INTO messages (name, email, message) VALUES ('" . $_POST['name'] . "', '" . $_POST['email'] . "', '" . $_POST['message'] . "')";
	// 执行 SQL 插入语句
	mysqli_query($con, $sql);
	// 关闭连接
	mysqli_close($con);
	// 重定向到当前页面,刷新留言列表
	header('Location: ' . $_SERVER['PHP_SELF']);
	exit;
}
?>
帮我用html写一个留言板

原文地址: https://www.cveoy.top/t/topic/C9J 著作权归作者所有。请勿转载和采集!

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