Successful connection to MySQL from HTML
Found a page: https://www.geeksforgeeks.org/retrieving-html-from-data-using-flask/ with a screen shot that parallels what I want to do - in terms of connecting to the DB.
Below is the screen shot that I was able to implement in FLASH_APP.PY and ThreeIndex.html in VDP.
@app.route('/Threeindex', methods =["GET", "POST"])
def Threeindex():
"""Renders the home page."""
from datetime import date, datetime, timedelta
import mysql.connector
import sys
if request.method == "POST":
# getting input with name = fname in HTML form
first_name = request.form.get("fname")
print('InfoForm a debugginng message 2 ', file=sys.stderr)
print(first_name, file=sys.stderr)
# getting input with name = lname in HTML form
last_name = request.form.get("lname")
#--extracted from Twoindex
print('Before the DB Connect', file=sys.stderr)
cnx = mysql.connector.connect(host='BenConnolly.mysql.pythonanywhere-services.com', user='BenConnolly', port=3306, password ='****',database='BenConnolly$VoterDecisionProject')
cursor = cnx.cursor()
print('before the SQL syntax ' , file=sys.stderr)
add_bat = ("INSERT INTO batches (BatchNumber, SendOutLabID, Status) VALUES (%s, %s, %s)")
print('Loading the Firstname into the Status field ' , file=sys.stderr)
data_bat = ('CCL-RM-20788', 9, first_name)
# Insert new employee
cursor.execute(add_bat, data_bat)
print('After the execution ' , file=sys.stderr)
# Make sure data is committed to the database
cnx.commit()
cursor.close()
cnx.close()
return render_template('thankyou.html',first=first_name)
return render_template('ThreeIndex.html')
Theoretically, this kind of loop continues until both users accept the same response.
Comments
Post a Comment