帮我写一个链接网页有增删改查功能的脚本
时间: 2023-12-15 08:03:46 浏览: 72
好的,这里提供一个简单的增删改查功能的脚本示例,使用PHP和MySQL实现:
1. 创建数据库和数据表
首先需要创建一个名为 "mydb" 的数据库,并在其中创建一个名为 "users" 的数据表,包含 "id"、"name"、"email" 和 "phone" 四个字段。
```SQL
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50),
phone VARCHAR(20)
);
```
2. 编写网页代码
通过 HTML、CSS 和 JavaScript 编写一个简单的网页,包含一个表格用于显示用户信息和四个按钮用于添加、修改、删除和查询操作。
```HTML
<html>
<head>
<title>User Management</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
<script>
function addUser() {
// TODO: 实现添加用户操作
}
function editUser(id) {
// TODO: 实现修改用户操作
}
function deleteUser(id) {
// TODO: 实现删除用户操作
}
function searchUser() {
// TODO: 实现查询用户操作
}
</script>
</head>
<body>
<h1>User Management</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Actions</th>
</tr>
<!-- TODO: 动态生成表格内容 -->
</table>
<br>
<button onclick="addUser()">Add User</button>
<button onclick="editUser()">Edit User</button>
<button onclick="deleteUser()">Delete User</button>
<button onclick="searchUser()">Search User</button>
</body>
</html>
```
3. 实现服务器端代码
使用 PHP 编写服务器端代码,通过 MySQL 连接数据库,实现添加、修改、删除和查询用户信息的操作。
```PHP
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "mydb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 添加用户
if (isset($_POST["add"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$sql = "INSERT INTO users (name, email, phone) VALUES ('$name', '$email', '$phone')";
mysqli_query($conn, $sql);
}
// 修改用户
if (isset($_POST["edit"])) {
$id = $_POST["id"];
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$sql = "UPDATE users SET name='$name', email='$email', phone='$phone' WHERE id=$id";
mysqli_query($conn, $sql);
}
// 删除用户
if (isset($_POST["delete"])) {
$id = $_POST["id"];
$sql = "DELETE FROM users WHERE id=$id";
mysqli_query($conn, $sql);
}
// 查询用户
if (isset($_POST["search"])) {
$keyword = $_POST["keyword"];
$sql = "SELECT * FROM users WHERE name LIKE '%$keyword%' OR email LIKE '%$keyword%' OR phone LIKE '%$keyword%'";
$result = mysqli_query($conn, $sql);
$users = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
// 查询所有用户
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$users = mysqli_fetch_all($result, MYSQLI_ASSOC);
// 输出 JSON 格式数据
echo json_encode($users);
// 关闭数据库连接
mysqli_close($conn);
?>
```
4. 实现客户端代码
使用 JavaScript 编写客户端代码,通过 AJAX 技术调用服务器端接口,实现动态生成表格内容和增删改查操作。
```JavaScript
function loadUsers() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "user.php", true);
xhr.onload = function() {
var users = JSON.parse(xhr.responseText);
var html = "";
for (var i = 0; i < users.length; i++) {
var user = users[i];
html += "<tr>";
html += "<td>" + user.id + "</td>";
html += "<td>" + user.name + "</td>";
html += "<td>" + user.email + "</td>";
html += "<td>" + user.phone + "</td>";
html += "<td><button onclick='editUser(" + user.id + ")'>Edit</button><button onclick='deleteUser(" + user.id + ")'>Delete</button></td>";
html += "</tr>";
}
document.querySelector("table").innerHTML += html;
};
xhr.send();
}
function addUser() {
var name = prompt("Please enter user name:");
var email = prompt("Please enter user email:");
var phone = prompt("Please enter user phone:");
var xhr = new XMLHttpRequest();
xhr.open("POST", "user.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("add=1&name=" + name + "&email=" + email + "&phone=" + phone);
location.reload();
}
function editUser(id) {
var name = prompt("Please enter new user name:");
var email = prompt("Please enter new user email:");
var phone = prompt("Please enter new user phone:");
var xhr = new XMLHttpRequest();
xhr.open("POST", "user.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("edit=1&id=" + id + "&name=" + name + "&email=" + email + "&phone=" + phone);
location.reload();
}
function deleteUser(id) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "user.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("delete=1&id=" + id);
location.reload();
}
function searchUser() {
var keyword = prompt("Please enter keyword:");
var xhr = new XMLHttpRequest();
xhr.open("POST", "user.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onload = function() {
var users = JSON.parse(xhr.responseText);
var html = "";
for (var i = 0; i < users.length; i++) {
var user = users[i];
html += "<tr>";
html += "<td>" + user.id + "</td>";
html += "<td>" + user.name + "</td>";
html += "<td>" + user.email + "</td>";
html += "<td>" + user.phone + "</td>";
html += "<td><button onclick='editUser(" + user.id + ")'>Edit</button><button onclick='deleteUser(" + user.id + ")'>Delete</button></td>";
html += "</tr>";
}
document.querySelector("table").innerHTML = "<tr><th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th>Actions</th></tr>" + html;
};
xhr.send("search=1&keyword=" + keyword);
}
loadUsers();
```
以上是一个简单的增删改查功能脚本示例,具体实现方式可能因环境和需求不同而略有差异。
阅读全文