从HTML表单获取输入并通过Flask将其存储在MySQL中 [英] Getting the input from HTML form and storing it in mysql through Flask
问题描述
html表格sample.html:
<!DOCTYPE html>
< html>
< body>
< form method =POST>
名字:< input type =textname =fname>< br>
姓氏:< input type =textname =lname>< br>
电子邮件地址:< input type =textname =emailid>< br>
< input type =submitvalue =Submit>
< / form>
< / body>
< / html>
正在使用的python脚本是:
<$从烧瓶导入烧瓶,请求,渲染模板
从flaskext.mysql导入MySQL
mysql = MySQL()
app = Flask(__ name__ )
app.config ['MYSQL_DATABASE_USER'] ='root'
app.config ['MYSQL_DATABASE_PASSWORD'] ='root'
app.config ['MYSQL_DATABASE_DB'] ='names'
app.config ['MYSQL_DATABASE_host'] ='127.0.0.1:3306'
mysql.init_app(app)
@ app.route('/',methods = [' GET','POST'])
def get_data():
返回render_template(sample.html)
if request.method =='POST':
first_name = request.form ['fname']
last_name = request.form ['lname']
emailid = request.form ['emailid']
connection = mysql.get_db()
cursor = connection.cursor()
query =INSERT INTO names_tbl(f_name,l_name,e_id)VALUES(%s,%s,%s)
cursor.execute(query,(first_name, last_name,email_id))
连接.commit()
返回nothing fucked
else:
return(something fucked up)
if __name __ =='__ main__':
app.run(debug = True)
<$ p $ import MySQLdb
$ b $ db = MySQLdb.connect($ b $ host ='localhost',
user ='root',
passwd = 'pw',
db ='mydB',
charset ='utf8'
)
@ app.route('/',methods = ['GET ','POST'])
def get_data():
返回render_template(sample.html)$ b $如果request.method =='POST':
first_name = request .form ['fname']
last_name = request.form ['lname']
emailid = request.form ['emailid']
cursor = db.cursor()
cursor.execute(
INSERT INTO names_tbl(f_name,l_name,e_id)\
VALUES(%s,%s,%s),(first_name,last_name,email_id))
cursor.close()
返回nothing fucked
if __name __ =='__ main__':
app.run(debug = True)
Am taking my first steps in python. Am using Flask framework and am writing a basic app that would take the input from the html form and store it in a table MySql database. So this is what am doing
The html form sample.html:
<!DOCTYPE html>
<html>
<body>
<form method="POST">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
Email Id : <input type="text" name="emailid"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
The python script am using is:
from flask import Flask,request,render_template
from flaskext.mysql import MySQL
mysql=MySQL()
app=Flask(__name__)
app.config['MYSQL_DATABASE_USER']='root'
app.config['MYSQL_DATABASE_PASSWORD']='root'
app.config['MYSQL_DATABASE_DB']='names'
app.config['MYSQL_DATABASE_host']='127.0.0.1:3306'
mysql.init_app(app)
@app.route('/',methods=['GET','POST'])
def get_data():
return render_template("sample.html")
if request.method=='POST':
first_name=request.form['fname']
last_name=request.form['lname']
emailid=request.form['emailid']
connection = mysql.get_db()
cursor = connection.cursor()
query="INSERT INTO names_tbl(f_name,l_name,e_id) VALUES(%s,%s,%s)"
cursor.execute(query,(first_name,last_name,email_id))
connection.commit()
return "nothing fucked"
else:
return("something fucked up")
if __name__=='__main__':
app.run(debug=True)
import MySQLdb
db = MySQLdb.connect(
host = 'localhost',
user = 'root',
passwd = 'pw',
db = 'mydB',
charset='utf8'
)
@app.route('/',methods=['GET','POST'])
def get_data():
return render_template("sample.html")
if request.method=='POST':
first_name=request.form['fname']
last_name=request.form['lname']
emailid=request.form['emailid']
cursor = db.cursor()
cursor.execute("""
INSERT INTO names_tbl(f_name,l_name,e_id) \
VALUES (%s,%s,%s) """, (first_name,last_name,email_id))
cursor.close()
return "nothing fucked"
if __name__=='__main__':
app.run(debug=True)
这篇关于从HTML表单获取输入并通过Flask将其存储在MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!