用python,html和MySQL写一个运输管理系统,这个系统可以根据mysql进行验证登录,还包括订单管理,车辆调度和车辆跟踪三个页面,这三个页面都可以进行数据的增加,删除和修改,并信息显示在其所在页面上
时间: 2023-11-21 15:05:31 浏览: 107
好的,这个系统需要分为前端和后端两部分,前端使用HTML、CSS、JavaScript等技术,后端使用Python和MySQL数据库。
首先需要安装Python和MySQL,并创建一个名为“transport_management”的数据库和三个表:user、order和vehicle。
代码如下:
```sql
CREATE DATABASE transport_management;
USE transport_management;
CREATE TABLE user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE order (
id INT PRIMARY KEY AUTO_INCREMENT,
order_no VARCHAR(255) UNIQUE NOT NULL,
sender_name VARCHAR(255) NOT NULL,
sender_address VARCHAR(255) NOT NULL,
receiver_name VARCHAR(255) NOT NULL,
receiver_address VARCHAR(255) NOT NULL,
status VARCHAR(255) NOT NULL
);
CREATE TABLE vehicle (
id INT PRIMARY KEY AUTO_INCREMENT,
vehicle_no VARCHAR(255) UNIQUE NOT NULL,
driver_name VARCHAR(255) NOT NULL,
driver_phone VARCHAR(255) NOT NULL,
status VARCHAR(255) NOT NULL
);
```
接下来编写后端代码:
```python
from flask import Flask, render_template, request, redirect, url_for, session
import mysql.connector
app = Flask(__name__)
app.secret_key = 'secret_key'
# 数据库连接配置
db_config = {
'host': 'localhost',
'user': 'root',
'password': 'password',
'database': 'transport_management'
}
# 登录页面
@app.route('/')
def login():
if 'username' in session:
return redirect(url_for('index'))
else:
return render_template('login.html')
# 登录验证
@app.route('/login', methods=['POST'])
def login_check():
username = request.form['username']
password = request.form['password']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('SELECT * FROM user WHERE username=%s AND password=%s', (username, password))
user = cursor.fetchone()
if user:
session['username'] = user[1]
return redirect(url_for('index'))
else:
return redirect(url_for('login'))
# 退出登录
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))
# 首页
@app.route('/index')
def index():
if 'username' in session:
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('SELECT * FROM order')
orders = cursor.fetchall()
cursor.execute('SELECT * FROM vehicle')
vehicles = cursor.fetchall()
return render_template('index.html', orders=orders, vehicles=vehicles)
else:
return redirect(url_for('login'))
# 订单管理页面
@app.route('/order')
def order():
if 'username' in session:
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('SELECT * FROM order')
orders = cursor.fetchall()
return render_template('order.html', orders=orders)
else:
return redirect(url_for('login'))
# 添加订单
@app.route('/add_order', methods=['POST'])
def add_order():
if 'username' in session:
order_no = request.form['order_no']
sender_name = request.form['sender_name']
sender_address = request.form['sender_address']
receiver_name = request.form['receiver_name']
receiver_address = request.form['receiver_address']
status = request.form['status']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('INSERT INTO order (order_no, sender_name, sender_address, receiver_name, receiver_address, status) VALUES (%s, %s, %s, %s, %s, %s)', (order_no, sender_name, sender_address, receiver_name, receiver_address, status))
db.commit()
return redirect(url_for('order'))
else:
return redirect(url_for('login'))
# 修改订单
@app.route('/update_order', methods=['POST'])
def update_order():
if 'username' in session:
id = request.form['id']
order_no = request.form['order_no']
sender_name = request.form['sender_name']
sender_address = request.form['sender_address']
receiver_name = request.form['receiver_name']
receiver_address = request.form['receiver_address']
status = request.form['status']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('UPDATE order SET order_no=%s, sender_name=%s, sender_address=%s, receiver_name=%s, receiver_address=%s, status=%s WHERE id=%s', (order_no, sender_name, sender_address, receiver_name, receiver_address, status, id))
db.commit()
return redirect(url_for('order'))
else:
return redirect(url_for('login'))
# 删除订单
@app.route('/delete_order', methods=['POST'])
def delete_order():
if 'username' in session:
id = request.form['id']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('DELETE FROM order WHERE id=%s', (id,))
db.commit()
return redirect(url_for('order'))
else:
return redirect(url_for('login'))
# 车辆调度页面
@app.route('/vehicle')
def vehicle():
if 'username' in session:
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('SELECT * FROM vehicle')
vehicles = cursor.fetchall()
return render_template('vehicle.html', vehicles=vehicles)
else:
return redirect(url_for('login'))
# 添加车辆
@app.route('/add_vehicle', methods=['POST'])
def add_vehicle():
if 'username' in session:
vehicle_no = request.form['vehicle_no']
driver_name = request.form['driver_name']
driver_phone = request.form['driver_phone']
status = request.form['status']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('INSERT INTO vehicle (vehicle_no, driver_name, driver_phone, status) VALUES (%s, %s, %s, %s)', (vehicle_no, driver_name, driver_phone, status))
db.commit()
return redirect(url_for('vehicle'))
else:
return redirect(url_for('login'))
# 修改车辆
@app.route('/update_vehicle', methods=['POST'])
def update_vehicle():
if 'username' in session:
id = request.form['id']
vehicle_no = request.form['vehicle_no']
driver_name = request.form['driver_name']
driver_phone = request.form['driver_phone']
status = request.form['status']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('UPDATE vehicle SET vehicle_no=%s, driver_name=%s, driver_phone=%s, status=%s WHERE id=%s', (vehicle_no, driver_name, driver_phone, status, id))
db.commit()
return redirect(url_for('vehicle'))
else:
return redirect(url_for('login'))
# 删除车辆
@app.route('/delete_vehicle', methods=['POST'])
def delete_vehicle():
if 'username' in session:
id = request.form['id']
db = mysql.connector.connect(**db_config)
cursor = db.cursor()
cursor.execute('DELETE FROM vehicle WHERE id=%s', (id,))
db.commit()
return redirect(url_for('vehicle'))
else:
return redirect(url_for('login'))
if __name__ == '__main__':
app.run()
```
接下来编写前端代码:
在templates文件夹中创建login.html、index.html、order.html和vehicle.html四个文件,分别对应登录页面、首页、订单管理页面和车辆调度页面。
login.html代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>运输管理系统 - 登录</title>
</head>
<body>
<form method="post" action="/login">
<h2>运输管理系统 - 登录</h2>
<div>
<label>用户名:</label>
<input type="text" name="username" required>
</div>
<div>
<label>密码:</label>
<input type="password" name="password" required>
</div>
<div>
<button type="submit">登录</button>
</div>
</form>
</body>
</html>
```
index.html代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>运输管理系统 - 首页</title>
</head>
<body>
<h2>欢迎 {{ session['username'] }} 登录运输管理系统</h2>
<div>
<h3>订单管理</h3>
<table border="1">
<tr>
<th>订单号</th>
<th>发件人姓名</th>
<th>发件人地址</th>
<th>收件人姓名</th>
<th>收件人地址</th>
<th>状态</th>
<th>操作</th>
</tr>
{% for order in orders %}
<tr>
<td>{{ order[1] }}</td>
<td>{{ order[2] }}</td>
<td>{{ order[3] }}</td>
<td>{{ order[4] }}</td>
<td>{{ order[5] }}</td>
<td>{{ order[6] }}</td>
<td>
<form method="post" action="/update_order">
<input type="hidden" name="id" value="{{ order[0] }}">
<button type="submit">修改</button>
</form>
<form method="post" action="/delete_order">
<input type="hidden" name="id" value="{{ order[0] }}">
<button type="submit">删除</button>
</form>
</td>
</tr>
{% endfor %}
</table>
<form method="post" action="/add_order">
<div>
<label>订单号:</label>
<input type="text" name="order_no" required>
</div>
<div>
<label>发件人姓名:</label>
<input type="text" name="sender_name" required>
</div>
<div>
<label>发件人地址:</label>
<input type="text" name="sender_address" required>
</div>
<div>
<label>收件人姓名:</label>
<input type="text" name="receiver_name" required>
</div>
<div>
<label>收件人地址:</label>
<input type="text" name="receiver_address" required>
</div>
<div>
<label>状态:</label>
<select name="status">
<option value="未发货">未发货</option>
<option value="已发货">已发货</option>
<option value="已签收">已签收</option>
</select>
</div>
<div>
<button type="submit">添加订单</button>
</div>
</form>
</div>
<div>
<h3>车辆调度</h3>
<table border="1">
<tr>
<th>车牌号</th>
<th>驾驶员姓名</th>
<th>驾驶员电话</th>
<th>状态</th>
<th>操作</th>
</tr>
{% for vehicle in vehicles %}
<tr>
<td>{{ vehicle[1] }}</td>
<td>{{ vehicle[2] }}</td>
<td>{{ vehicle[3] }}</td>
<td>{{ vehicle[4] }}</td>
<td>
<form method="post" action="/update_vehicle">
<input type="hidden" name="id" value="{{ vehicle[0] }}">
<button type="submit">修改</button>
</form>
<form method="post" action="/delete_vehicle">
<input type="hidden" name="id" value="{{ vehicle[0] }}">
<button type="submit">删除</button>
</form>
</td>
</tr>
{% endfor %}
</table>
<form method="post" action="/add_vehicle">
<div>
<label>车牌号:</label>
<input type="text" name="vehicle_no" required>
</div>
<div>
<label>驾驶员姓名:</label>
<input type="text" name="driver_name" required>
</div>
<div>
<label>驾驶员电话:</label>
<input type="text" name="driver_phone" required>
</div>
<div>
<label>状态:</label>
<select name="status">
<option value="未出车">未出车</option>
<option value="已出车">已出车</option>
<option value="已回车">已回车</option>
</select>
</div>
<div>
<button type="submit">添加车辆</button>
</div>
</form>
</div>
<form method="post" action="/logout">
<button type="submit">退出登录</button>
</form>
</body>
</html>
```
order.html代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>运输管理系统 - 订单管理</title>
</head>
<body>
<h2>订单管理</h2>
<table border="1">
<tr>
<th>订单号</th>
<th>发件人姓名</th>
<th>发件人地址</th>
<th>收件人姓名</th>
<th>收件人地址</th>
<th>状态</th>
<th>操作</th>
</tr>
{% for order in orders %}
<tr>
<td>{{ order[1] }}</td>
<td>{{ order[2] }}</td>
<td>{{ order[3] }}</td>
<td>{{ order[4] }}</td>
<td>{{ order[5] }}</td>
<td>{{ order[6] }}</td>
<td>
<form method="post" action="/update_order">
<input type="hidden" name="id" value="{{ order[0] }}">
<button type="submit">修改</button>
</form>
<form method="post" action="/delete_order">
<input type="hidden" name="id" value="{{ order[0] }}">
<button type="submit">删除</button>
</form>
</td>
</tr>
{% endfor %}
</table>
<form method="post" action="/add_order">
<div>
<label>订单号:</label>
<input type="text" name="order_no" required>
</div>
<div>
<label>发件人姓名:</label>
<input type="text" name="sender_name" required>
</div>
<div>
<label>发件人地址:</label>
<input type="text" name="sender_address" required>
</div>
<div>
<label>收件人姓名:</label>
<input type="text" name="receiver_name" required>
</div>
<div>
<label>收件人地址:</label>
<input type="text" name="receiver_address" required>
</div>
<div>
<label>状态:</label>
<select name="status">
<option value="未发货">未发货</option>
<option value="已发货">已发货</option>
<option value="已签收">已签收</option>
</select>
</div>
<div>
<button type="submit">添加订单</button>
</div>
</form>
<form method="post" action="/index">
<button type="submit">返回首页</button>
</form>
</body>
</html>
```
vehicle.html代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>运输管理系统 - 车辆调度</title>
</head>
<body>
<h2>车辆调度</h2>
<table border="1">
<tr>
<th>车牌号</th>
<th>驾驶员姓名</th>
<th>驾驶员电话</th>
<th>状态</th>
<th>操作</th>
</tr>
{% for vehicle in vehicles %}
<tr>
<td>{{ vehicle[1] }}</td>
<td>{{ vehicle[2] }}</td>
<td>{{ vehicle[3] }}</td>
<td>{{ vehicle[4] }}</td>
<td>
<form method="post" action="/update_vehicle">
<input type="hidden" name="id" value="{{ vehicle[0] }}">
<button type="submit">修改</button>
</form>
<form method="post" action="/delete_vehicle">
<input type="hidden" name="id" value="{{ vehicle[0] }}">
<button type="submit">删除</button>
</form>
</td>
</tr>
{% endfor %}
</table>
<form method="post" action="/add_vehicle">
<div>
<label>车牌号:</label>
<input type="text" name="vehicle_no"
阅读全文