请帮我利用php语言写一份用户列表的网页要求从数据库中调取字段id、name、age、xh、class、phonenumber并且写一个搜索栏可以根据name字段进行数据查询并且显示结果要求做的精美一些并且标注相应的代码注释
时间: 2024-01-25 17:05:07 浏览: 81
好的,以下是你需要的代码。注释已经添加:
```php
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
border-bottom: 1px solid #ddd;
}
tr:hover {background-color:#f5f5f5;}
</style>
</head>
<body>
<h1>User List</h1>
<!-- 搜索栏 -->
<form method="GET">
<label for="search">Search by Name:</label>
<input type="text" name="search" id="search" value="<?php echo isset($_GET['search']) ? $_GET['search'] : ''; ?>">
<button type="submit">Search</button>
</form>
<!-- 用户列表 -->
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>XH</th>
<th>Class</th>
<th>Phone Number</th>
</tr>
</thead>
<tbody>
<?php
// 数据库连接
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 查询语句
$sql = "SELECT id, name, age, xh, class, phonenumber FROM users";
// 如果有搜索条件,添加 WHERE 子句
if (isset($_GET['search'])) {
$search = mysqli_real_escape_string($conn, $_GET['search']);
$sql .= " WHERE name LIKE '%{$search}%'";
}
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['xh'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='6'>No users found.</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</body>
</html>
```
需要注意的是,你需要替换代码中的以下内容:
- `$servername`:数据库的主机名;
- `$username`:数据库的用户名;
- `$password`:数据库的密码;
- `$dbname`:数据库的名称;
- `name`、`age`、`xh`、`class`、`phonenumber`:数据库表格中的字段名称。
此外,还需要在服务器上安装并配置 PHP 环境,以及在本地或者服务器上安装并配置 MySQL 数据库。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)