我在HTML中有三个Checkbox,我在Python中获得了复选框值。如果我选择Checkbox意味着值应插入为"是"在MYSQL数据库中,否则它应该插入"否"。我曾尝试但我不知道如何获得答案。请帮助我,在此先感谢:)
这是我的代码: 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>
吡啶:
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()
答案 0 :(得分:0)
首先,为什么要命名像“check”,“check”和“chec”这样的字段,它们似乎都是name =“checks []”,或者它的命名风格很差。
你是不是想要获得所有三个字段的值?那你应该试着让他们三个中的Results
合适吗?
到目前为止,您还可以发布您的输出结果吗?
答案 1 :(得分:0)
<label for="">Amenities Required</label><br>
<input type="checkbox" name="users" value="Bike" checked> I have a bike<br>
<input type="checkbox" name="users" value="Car"> I have a car<br>
<input type="checkbox" name="users" value="Boat" > I have a boat<br>
<input type="checkbox" name="users" value="cycle"> I have a cycle<br>
<input type="checkbox" name="users" value="wagnor"> I have a wagnor<br>
<input type="checkbox" name="users" value="safari" > I have a safari<br>
if request.method=='POST':
amin=request.form.getlist("users")
str1 = ','.join(amin)
c.execute("INSERT INTO tablename VALUES(%s)",[str1])
答案 2 :(得分:0)
'''
<div class="row">
<div class="col-lg-4 col-sm-6">
<input type="checkbox" name="clinic_info" value="Clinial abnormalities"> Clinial abnormalities
</div>
<div class="col-lg-4 col-sm-6">
<input type="checkbox" name="clinic_info" value="Cytological abnormalities"> Cytological abnormalities
</div>
<div class="col-lg-4 col-sm-6">
<input type="checkbox" name="clinic_info" value="Low grade(L-SL)HPV"> Low grade(L-SL)HPV
</div>
</div>
'''
@app.route('/index', methods=['POST','GET'])
def index():
if request.method == 'POST':
clinical_info = request.form.getlist('clinic_info')
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
clinical_info = ','.join(clinical_info)
values = (clinical_info)
cursor.execute('INSERT INTO table (table_name) VALUES (%s)',values)
mysql.connection.commit()
return 'You have successfully send to mysql database!'
return render_template('clinical.html')