PHP网页操作MySQL数据库 - 轻松管理zhidemaikey表
<!DOCTYPE html>
<html>
<head>
<title>操作MySQL数据库 - zhidemaikey表</title>
<style>
body{
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.container{
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
h1{
text-align: center;
color: #333;
}
table{
width: 100%;
border-collapse: collapse;
}
table td, table th{
padding: 10px;
border: 1px solid #ccc;
}
.add-form{
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.add-form input{
flex-grow: 1;
padding: 5px;
margin-right: 10px;
}
.add-form button{
padding: 5px 10px;
background-color: #4CAF50;
color: #fff;
border: none;
cursor: pointer;
}
.delete-btn{
padding: 5px 10px;
background-color: #f44336;
color: #fff;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class='container'>
<h1>操作MySQL数据库 - zhidemaikey表</h1>
<pre><code> <div class='add-form'>
<input type='text' id='newItem' placeholder='输入要添加的内容'>
<button onclick='addItem()'>添加</button>
</div>
<?php
// 数据库配置
$host = '43.143.25.74';
$port = 3306;
$user = 'tuisong';
$passwd = 'xX300400';
$db = 'tuisong';
$charset = 'utf8';
// 连接数据库
$conn = new mysqli($host, $user, $passwd, $db, $port);
if ($conn->connect_error) {
die("连接数据库失败: " . $conn->connect_error);
}
// 查询操作
$sql = "SELECT * FROM zhidemaikey";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><th>内容</th><th>操作</th></tr>";
while ($row = $result->fetch_assoc()) {
$zdmkey = $row["zdmkey"];
$id = $row["id"];
echo "<tr><td>" . $zdmkey . "</td><td><button class='delete-btn' onclick='deleteItem("' . $zdmkey . '")'>删除</button></td></tr>";
}
echo "</table>";
} else {
echo "暂无数据";
}
// 关闭数据库连接
$conn->close();
?>
<script>
// 删除操作
function deleteItem(zdmkey){
if (confirm("确定要删除吗?")) {
window.location.href = "delete.php?zdmkey=" + zdmkey;
}
}
// 添加操作
function addItem(){
var newItem = document.getElementById("newItem").value;
if (newItem !== "") {
window.location.href = "add.php?zdmkey=" + newItem;
}
}
</script>
</div>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/o6tT 著作权归作者所有。请勿转载和采集!