gpt4 book ai didi

python - 如何使用 Flask 在 MYSQL 中插入复选框值

转载 作者:行者123 更新时间:2023-11-29 18:11:29 24 4
gpt4 key购买 nike

我在 HTML 中有三个复选框,并且在 Python 中获得了复选框值。如果我选​​择该复选框,则意味着该值应在 MYSQL 数据库中插入为"is",否则应插入为“否”。我已尝试过,但是我不知道如何得到答案。请帮助我,提前致谢:)

这是我的代码:HTML:

<form action='/hello' method="POST" enctype="multipart/form-data">
Panchayath <input type="checkbox" name="checks[]" value="yes" id="panchayath"><br>
Ward <input type="checkbox" name="check[]" value=" yes" id=" ward"><br>
Results <input type="checkbox" name="chec[]" value=" yes" id=" results">
<input type="submit" value="Submit">
</form>

py:

import os
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'] = 'bucketlist'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))


@app.route('/hello', methods=['GET', 'POST'])
def get_data():
print("hlo")
if request.method == 'POST':
print("cat")
panchayath = request.form.getlist('checks[]')
print panchayath
ward = request.form.getlist('check[]')
print ward
Results = request.form.getlist('chec[]')
print Results
conn = mysql.connect()
cursor = conn.cursor()
# cursor.execute()
print "yes"
# cursor.execute("insert into try_chk3 values (%s,%s,%s)", (some_var))
# query = "CREATE TABLE saracheck( panchayath VARCHAR(40), ward VARCHAR(40),Results VARCHAR (40))"
# cursor.execute(query)
query = "INSERT INTO saracheck(panchayath,ward,Results) VALUES(%s,%s,%s)"
a=cursor.execute(query, (panchayath,ward,Results))
print a
print "execuet"
# cursor.execute(query,(first_name,last_name,email_id,can_pic,logo_pic))
conn.commit()


return render_template("form.html")


if __name__ == '__main__':
app.run()

输出: enter image description here

最佳答案

首先,为什么要命名“checks”、“check”和“chec”等字段,它们要么看起来都是 name =“checks[]”,要么就是糟糕的命名方式。

您是否还想获取所有三个字段的值?那么您应该尝试获取所有三个的结果,对吧?

您能否也发布您到目前为止所得到的输出?

关于python - 如何使用 Flask 在 MYSQL 中插入复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com