赞
踩
其中代码段coon里面的db换成自己的数据库的名字,host和passed换成自己mysql的用户和密码。
flask代码如下:
- from flask import Flask, render_template ,request
- import pymysql
- app = Flask(__name__)
-
- @app.route('/', methods = ["GET", "POST"])
- def index():
- if request.method == "GET":
- return render_template('index.html')
-
- username = request.form.get("user")
- password = request.form.get("password")
- mobile = request.form.get("mobile")
- print(username,password,mobile)
-
- #连接mysql
- conn = pymysql.connect(host="localhost", user='root', passwd="Mysql123", charset='utf8', db='unicom')
- cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
- #发送命令
- sql = "insert into admin(username, password, mobile) values(%s, %s, %s)"
- cursor.execute(sql, [username, password, mobile])
- conn.commit()
- #关闭
- cursor.close()
- conn.close()
-
- return 'thank you uesing'
-
- if __name__ == '__main__':
- app.run()
html代码如下:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <h1>添加用户</h1>
- <form method="post">
- <input type="text" name="user" placeholder="用户名">
- <input type="text" name="password" placeholder="密码">
- <input type="text" name="mobile" placeholder="手机号">
- <input type="submit" value="提交">
- </form>
-
-
-
-
- </body>
- </html>
运行然后打开网站:
接着输入数据提交:
然后去数据库查看有没有添加成功:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。