已有一个php页面能实现上传留言功能。再设计php页面,一个页面对留言进行列表,以表格的形式展示留言 的简介;当点击一条信息后,显示该留言的详细信息
时间: 2023-12-10 20:42:18 浏览: 29
好的,以下是详细步骤:
1. 首先,我们需要创建一个 MySQL 数据库来存储留言信息。在该数据库中创建一个名为 messages 的表,包含以下字段:id(主键,自增)、name、email、title、message、created_at(时间戳)。
2. 接着,我们需要创建一个用于展示留言列表的 PHP 页面。该页面应该连接到数据库,并从 messages 表中获取所有留言的简介信息并以表格形式展示。每行应该包含留言的标题、作者和创建时间,并且应该具有一个链接以查看该留言的详细信息。
3. 点击链接后,应该跳转到另一个 PHP 页面,该页面应该显示该留言的详细信息。该页面应该连接到数据库,并从 messages 表中获取该留言的所有信息,包括作者、标题、内容和创建时间,并以易于阅读的格式呈现给用户。
以下是示例代码:
留言列表页面(messages.php):
```php
<?php
// 连接到数据库
$dsn = 'mysql:host=localhost;dbname=your_database_name';
$username = 'your_username';
$password = 'your_password';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
// 获取所有留言的简介信息
$stmt = $db->query('SELECT id, name, title, created_at FROM messages ORDER BY created_at DESC');
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>留言列表</title>
</head>
<body>
<h1>留言列表</h1>
<table>
<thead>
<tr>
<th>标题</th>
<th>作者</th>
<th>创建时间</th>
</tr>
</thead>
<tbody>
<?php foreach ($messages as $message): ?>
<tr>
<td><a href="message.php?id=<?php echo $message['id']; ?>"><?php echo $message['title']; ?></a></td>
<td><?php echo $message['name']; ?></td>
<td><?php echo date('Y-m-d H:i:s', $message['created_at']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
```
留言详细信息页面(message.php):
```php
<?php
// 连接到数据库
$dsn = 'mysql:host=localhost;dbname=your_database_name';
$username = 'your_username';
$password = 'your_password';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
// 获取留言的详细信息
$stmt = $db->prepare('SELECT name, email, title, message, created_at FROM messages WHERE id = ?');
$stmt->execute([$_GET['id']]);
$message = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $message['title']; ?></title>
</head>
<body>
<h1><?php echo $message['title']; ?></h1>
<p>作者:<?php echo $message['name']; ?></p>
<p>邮箱:<?php echo $message['email']; ?></p>
<p>创建时间:<?php echo date('Y-m-d H:i:s', $message['created_at']); ?></p>
<p><?php echo $message['message']; ?></p>
</body>
</html>
```
注意:在实际使用中,请将 your_database_name、your_username 和 your_password 替换为实际的数据库名称、用户名和密码。另外,为了安全起见,应该对用户提交的数据进行验证和过滤,以避免 SQL 注入等安全漏洞。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)