生成一个网页代码,可以连接数据库,数据库名称是test,用户是root,密码无,表是mytable,里面有三项内容id,type,content,展示数据库内容的框口并且可以进行数据库检索和筛选和添加删除,id会根据添加删除进行动态改变
时间: 2024-05-15 19:18:52 浏览: 3
好的,根据您的需求,以下是生成连接数据库test并展示内容的网页代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Database Connection</title>
</head>
<body>
<h1>My Database</h1>
<table>
<tr>
<th>ID</th>
<th>Type</th>
<th>Content</th>
</tr>
<?php
// Connect to database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Retrieve data from database
$sql = "SELECT * FROM mytable";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// Output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['type']."</td>";
echo "<td>".$row['content']."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</table>
<br>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="type">Type:</label>
<input type="text" id="type" name="type">
<label for="content">Content:</label>
<input type="text" id="content" name="content">
<input type="submit" value="Add">
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="id">ID:</label>
<input type="text" id="id" name="id">
<input type="submit" value="Delete">
</form>
<br>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="search">Search:</label>
<input type="text" id="search" name="search">
<select id="filter" name="filter">
<option value="id">ID</option>
<option value="type">Type</option>
<option value="content">Content</option>
</select>
<input type="submit" value="Search">
</form>
</body>
</html>
<?php
// Add data to database
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["type"]) && !empty($_POST["content"])) {
$type = $_POST["type"];
$content = $_POST["content"];
$sql = "INSERT INTO mytable (type, content) VALUES ('$type', '$content')";
if (mysqli_query($conn, $sql)) {
header("Refresh:0");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
// Delete data from database
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["id"])) {
$id = $_POST["id"];
$sql = "DELETE FROM mytable WHERE id=$id";
if (mysqli_query($conn, $sql)) {
header("Refresh:0");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
// Search data from database
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["search"]) && !empty($_POST["filter"])) {
$search = $_POST["search"];
$filter = $_POST["filter"];
$sql = "SELECT * FROM mytable WHERE $filter LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// Output data of each row
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Type</th>";
echo "<th>Content</th>";
echo "</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['type']."</td>";
echo "<td>".$row['content']."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
}
?>
```
该代码使用PHP语言,可连接名为test的MySQL数据库,用户名为root,密码为空,包含一个名为mytable的表,表有三项内容id,type,content。网页首先展示数据库的所有内容,并且提供了添加和删除数据的表单。添加数据的表单会在提交时执行INSERT语句将数据添加到表中,删除数据的表单会在提交时执行DELETE语句将指定id的数据从表中删除。网页还提供了一个搜索表单,可以根据用户选择的过滤条件在内容中搜索匹配的结果并展示在网页中。