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.


Below is the Threeindex routine from FLASK_APP.py:

@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')


Below is a screenshot of MySQL showing the record with a Status of "NEO" - that is what I entered as a first name on the "Puppy Survey" page


This technique will be useful for "SIGNUP" and CHAGPT questions at a minimum. The idea being to have a user ask a political question of CHATGPT - store the result and then to take the result and rephrase the response for an opposing user to accept or change. 

Theoretically, this kind of loop continues until both users accept the same response. 




Comments

Popular posts from this blog

Can I have AI create me a website to be used by voters?

The next step completed - at a milestone of sorts

Reset or level set the routine