写一个数据管理系统,前后端的连接,在前端实现对数据的增删查改
时间: 2024-03-10 13:50:12 浏览: 109
小白利用PHP+MYSQL+HTML做的期末小项目,实现后台与前端的连接,实现了增删改查功能,美化做的不好大家勿喷,还请多多指正
好的,我可以为你提供一些示例代码来实现这个数据管理系统。由于涉及到前后端的连接,需要使用一些常见的Web开发技术,比如HTML、CSS、JavaScript、Python等来实现。具体实现步骤如下:
1. 选择合适的数据库,并进行数据建模和设计,这里我选择使用MySQL数据库,并创建一个名为"users"的表,其中包含"id"、"name"、"email"三个字段。
2. 搭建后端服务器并编写API接口,这里我选择使用Python Flask框架来搭建后端服务器,并编写如下API接口:
```python
from flask import Flask, request, jsonify
import pymysql
app = Flask(__name__)
conn = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='test'
)
@app.route('/users', methods=['GET'])
def get_users():
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
users = cursor.fetchall()
cursor.close()
return jsonify(users)
@app.route('/users', methods=['POST'])
def add_user():
name = request.json.get('name')
email = request.json.get('email')
cursor = conn.cursor()
cursor.execute('INSERT INTO users (name, email) VALUES (%s, %s)', (name, email))
conn.commit()
cursor.close()
return jsonify({'message': 'User added successfully'})
@app.route('/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
name = request.json.get('name')
email = request.json.get('email')
cursor = conn.cursor()
cursor.execute('UPDATE users SET name=%s, email=%s WHERE id=%s', (name, email, user_id))
conn.commit()
cursor.close()
return jsonify({'message': 'User updated successfully'})
@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
cursor = conn.cursor()
cursor.execute('DELETE FROM users WHERE id=%s', user_id)
conn.commit()
cursor.close()
return jsonify({'message': 'User deleted successfully'})
if __name__ == '__main__':
app.run(debug=True)
```
3. 选择合适的前端框架并编写用户界面,这里我选择使用Vue.js框架来实现用户界面,并编写如下代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Management System</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1>User Management System</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>
<button @click="editUser(user)">Edit</button>
<button @click="deleteUser(user)">Delete</button>
</td>
</tr>
</tbody>
</table>
<form>
<h2 v-if="editing">Edit User</h2>
<h2 v-else>Add User</h2>
<label>Name:</label>
<input type="text" v-model="name">
<label>Email:</label>
<input type="email" v-model="email">
<button @click.prevent="editing ? updateUser() : addUser()">{{ editing ? 'Update' : 'Add' }}</button>
<button @click.prevent="cancelEdit()" v-if="editing">Cancel</button>
</form>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
users: [],
name: '',
email: '',
editing: false,
editingUser: null
},
mounted: function() {
this.getUsers();
},
methods: {
getUsers: function() {
fetch('/users')
.then(function(response) {
return response.json();
})
.then(function(data) {
app.users = data;
});
},
addUser: function() {
fetch('/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: app.name,
email: app.email
})
})
.then(function(response) {
app.getUsers();
app.name = '';
app.email = '';
});
},
editUser: function(user) {
app.editing = true;
app.editingUser = user;
app.name = user.name;
app.email = user.email;
},
updateUser: function() {
fetch('/users/' + app.editingUser.id, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: app.name,
email: app.email
})
})
.then(function(response) {
app.getUsers();
app.editing = false;
app.editingUser = null;
app.name = '';
app.email = '';
});
},
deleteUser: function(user) {
fetch('/users/' + user.id, {
method: 'DELETE'
})
.then(function(response) {
app.getUsers();
});
},
cancelEdit: function() {
app.editing = false;
app.editingUser = null;
app.name = '';
app.email = '';
}
}
});
</script>
</body>
</html>
```
综上所述,这个数据管理系统的实现就完成了,并可以实现用户界面上对数据的增删查改操作。当然,这只是一个简单的示例代码,具体实现还需要根据实际需求和技术栈进行调整和优化。
阅读全文