I am trying to insert values to the mysql table using flask. When I run the code, I get the html page, I enter the details and click the submit button. I have used 'post' method and used flask request to get the data and used that to sql table. But, I am getting Bad request error. Below is the code.
from flask import Flask, render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'HOTEL_MANAGEMENT'
mysql = MySQL(app)
@app.route('/', methods = ['GET', 'POST'])
def index():
if request.method == "POST":
# fetch form data
userDetails = request.form
Name = userDetails['Name']
Address = userDetails['Address']
Phone_no = userDetails['Phone_no']
User_id = userDetails['User_id']
Password = userDetails['Password']
# Store in database mycursor
mycursor = mysql.connection.cursor()
# use hotel management database
mycursor.execute("USE HOTEL_MANAGEMENT")
mycursor.execute("""
INSERT INTO Reg_details(Name, Address, Phone_no, User_id, Password)
VALUES('{}','{}','{}','{}','{}')
""".format(Name, Address, Phone_no, User_id, Password))
# Commit the database
mysql.connection.commit()
mycursor.close()
return 'success'
return render_template("user_details.html")
if __name__=='__main__':
app.run()
Error
Bad request (400) The browser (or proxy) sent a request that this server could not understand.
What might be wrong which is creating the error?