用python MySQL和html实现一个管理系统包括人员管理和员工管理两个页面,并且在页面里进行对数据的修改、添加、删除
时间: 2024-01-24 21:16:56 浏览: 26
首先,我们需要安装MySQL和Python的MySQL驱动程序。可以使用以下命令在终端中安装:
```
sudo apt-get install mysql-server python3-mysqldb
```
接下来,我们需要创建一个名为“employee_management”的数据库和两个表格“person”和“employee”。可以使用以下命令在MySQL中创建:
```
CREATE DATABASE employee_management;
USE employee_management;
CREATE TABLE person (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE employee (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
```
现在,我们可以开始编写Python代码。我们需要使用Flask框架来创建Web应用程序,并使用MySQL Python库来连接和操作数据库。
首先,我们需要导入必要的模块:
```python
from flask import Flask, render_template, request, redirect, url_for
import MySQLdb
```
然后,我们创建Flask应用程序并连接到MySQL数据库:
```python
app = Flask(__name__)
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="employee_management")
cursor = db.cursor()
```
现在,我们可以定义两个路由分别用于人员管理和员工管理页面。这些页面将从HTML文件中渲染,因此我们需要使用render_template函数来呈现它们。
```python
@app.route('/')
def index():
return render_template('index.html')
@app.route('/person')
def person():
cursor.execute("SELECT * FROM person")
data = cursor.fetchall()
return render_template('person.html', data=data)
@app.route('/employee')
def employee():
cursor.execute("SELECT * FROM employee")
data = cursor.fetchall()
return render_template('employee.html', data=data)
```
注意,我们使用SELECT查询来检索数据库中的所有数据,并将结果存储在data变量中。然后,我们将这些数据通过render_template函数传递到HTML文件中。
接下来,我们需要为每个页面创建一个HTML模板。对于员工管理和人员管理页面,我们需要显示数据库中的所有数据,并提供一些表单来添加、修改或删除数据。
例如,以下是person.html的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Person Management</title>
</head>
<body>
<h1>Person Management</h1>
<table border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
{% for row in data %}
<tr>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>
<a href="{{ url_for('edit_person', id=row[0]) }}">Edit</a> |
<a href="{{ url_for('delete_person', id=row[0]) }}">Delete</a>
</td>
</tr>
{% endfor %}
</table>
<hr>
<h2>Add Person</h2>
<form method="POST" action="{{ url_for('add_person') }}">
<label>Name:</label>
<input type="text" name="name">
<br>
<label>Email:</label>
<input type="text" name="email">
<br>
<input type="submit" value="Add">
</form>
</body>
</html>
```
此模板将显示一个表格,其中包含所有人员的数据。对于每一行,我们提供一个编辑和删除按钮。在页面底部,我们提供一个表单来添加新的人员数据。
我们还需要为每个路由定义相应的函数来处理表单提交和数据库操作。例如,以下是用于添加新人员的函数:
```python
@app.route('/add_person', methods=['POST'])
def add_person():
name = request.form['name']
email = request.form['email']
cursor.execute("INSERT INTO person (name, email) VALUES (%s, %s)", (name, email))
db.commit()
return redirect(url_for('person'))
```
此函数将从表单中获取姓名和电子邮件地址,并将其插入到person表中。然后,它将提交更改并重定向到person页面。
类似地,我们可以编写函数来处理编辑和删除请求。
最后,我们需要运行应用程序并在浏览器中访问它。可以使用以下代码运行应用程序:
```python
if __name__ == '__main__':
app.run(debug=True)
```
此代码将启动Flask应用程序并打开调试模式,以便在出现错误时显示详细信息。现在,您可以在浏览器中访问http://localhost:5000/来查看Web应用程序。