从HTML表单获取输入并通过Flask将其存储在MySQL中 [英] Getting the input from HTML form and storing it in mysql through Flask

查看:132
本文介绍了从HTML表单获取输入并通过Flask将其存储在MySQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在采取蟒蛇的第一步。我正在使用Flask框架,并正在写一个基本的应用程序,将采取从HTML表单的输入,并将其存储在一个表MySql数据库。所以这就是我在做什么

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆