Building a Simple Flask & MySQL Tool to Update User Information
This article demonstrates how to create a simple Flask‑based web tool that updates user information in a MySQL database, covering configuration, route handling, HTML form design, and execution steps, providing a practical example for backend developers.
When companies train new employees, the usual approach is to deliver theoretical business knowledge, which can feel uninspiring; creating small, practical tools offers a more engaging way to teach and demonstrate concepts.
Testing often requires generating data to simulate user conditions, such as modifying Redis or directly updating a database. This guide focuses on the latter, showing how to change user records in MySQL through a lightweight Flask application.
The core Python script ( main.py) sets up Flask, connects to MySQL, and defines two routes: one to display an update form and another to process the submitted data and execute an SQL UPDATE statement.
from flask import Flask, request, redirect, render_template
from flask_bootstrap import Bootstrap
from flaskext.mysql import MySQL
import pymysql
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = '用户名'
app.config['MYSQL_DATABASE_PASSWORD'] = '密码'
app.config['MYSQL_DATABASE_DB'] = '数据库名称'
app.config['MYSQL_DATABASE_HOST'] = 'host'
mysql.init_app(app)
bootstrap = Bootstrap(app)
# Connect to database
connect = mysql.connect()
cursor = connect.cursor()
# Show update page
@app.route('/update/')
def update():
# Get query parameters from the URL
userinfoId = request.args.get('userinfoId')
name = request.args.get('name')
return render_template('update.html')
# Process update action
@app.route('/updateaction/', methods=['POST'])
def updateaction():
params = request.args if request.method == 'GET' else request.form
userinfoId = params.get('userinfoId')
name = params.get('name')
sql = "update userinfo set name='%s' where id=%s" % (name, userinfoId)
cursor.execute(sql)
connect.commit()
return redirect('/index')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)The accompanying HTML template ( update.html) provides a simple form where the user can input the target userinfoId and the new name value.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>更新用户余额</title>
</head>
<body>
<form action="/updateaction/" method="post">
userinfoId: <input type="text" name="userinfoId" value=""/>
name: <input type="text" name="name" value=""/>
<input type="submit" value="update">
</form>
</body>
</html>To run the tool, execute python main.py runserver. The web interface appears, allowing you to enter the user ID and the new name; after submission, the database is updated and you are redirected to the index page. A screenshot (not shown here) illustrates the successful update.
By combining a few lines of Python and a minimal HTML form, developers can quickly create internal utilities that automate routine business tasks, and these utilities can be expanded into more comprehensive tools as needs grow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
