在线记事本 - 简单易用,表格显示,添加删除笔记
<!DOCTYPE html>
<html>
<head>
<title>在线记事本 - 简单易用</title>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
<pre><code> h1 {
text-align: center;
margin-top: 50px;
}
.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
}
.form {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
.form label, .form input[type="submit"] {
margin: 10px 0;
font-size: 1.2rem;
}
.form input[type="text"] {
padding: 5px;
font-size: 1.2rem;
border-radius: 5px;
border: 1px solid #ccc;
width: 100%;
max-width: 500px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table th, table td {
padding: 10px;
border: 1px solid #ccc;
text-align: center;
}
table th {
background-color: #eee;
}
table td button {
background-color: #f44336;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
}
</style>
</code></pre>
</head>
<body>
<h1>在线记事本</h1>
<div class="container">
<form class="form" action="" method="POST">
<label for="note">添加新的笔记:</label>
<input type="text" id="note" name="note" required>
<input type="submit" value="添加">
</form>
<pre><code> <table>
<thead>
<tr>
<th>序号</th>
<th>笔记内容</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
// 数据库配置
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "notes";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 添加新笔记
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$note = $_POST["note"];
$sql = "INSERT INTO notes (note) VALUES ('$note')";
if ($conn->query($sql) === TRUE) {
echo "<script>alert('新笔记添加成功!'); window.location.href='';</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// 查询已有笔记
$sql = "SELECT * FROM notes";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
$i = 1;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $row["note"] . "</td>";
echo "<td><button onclick='deleteNote(" . $row["id"] . ")'>删除</button></td>";
echo "</tr>";
$i++;
}
} else {
echo "<tr><td colspan='3'>暂无笔记</td></tr>";
}
$conn->close();
?>
</tbody>
</table>
</div>
<script type="text/javascript">
// 删除笔记
function deleteNote (id) {
var confirmDelete = confirm("确定要删除这条笔记吗?");
if (confirmDelete == true) {
window.location.href = "delete.php?id=" + id;
}
}
</script>
</code></pre>
</body>
</html>
<!-- delete.php 文件 -->
<?php
// 数据库配置
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "notes";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 删除笔记
$id = $_GET["id"];
$sql = "DELETE FROM notes WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "<script>alert('笔记删除成功!'); window.location.href='';</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
原文地址: https://www.cveoy.top/t/topic/neVP 著作权归作者所有。请勿转载和采集!