I have been at this for about an hour and a half now, and I have not been able to devise a solution. I have been working with the following code:
form = cgi.FieldStorage()
usrName = form.getvalue('uname')
usrPW = form.getvalue('psw')
while(usrPW == None):
form = cgi.FieldStorage()
usrName = form.getvalue('uname')
usrPW = form.getvalue('psw')
#usrName = str(usrName)
# usrPW = str(usrPW)
connection = pymysql.connect(host = 'localhost', user='golden', password='password', db='db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
#create a new record in the database
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, (usrName, usrPW))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
This is my attempt to pass data from my form, named uname and psw, respectively. This code worked when I had hard coded the values for uname and psw in the cursor.execute() call, however, when I try to send the form data to the query I get one of two results:
404 with unable to find file
If I remove the
while(usrPW == None):at the top (and the commands within the while loop), it executes immediately after the page is loaded- before I can type anything in and passes in nothing to both parameters, causing an error.
Any thoughts on how to fix this??
How do I properly pass data from a python CGI form to a mySQL database?
EDIT: Traceback update.
Provided I comment out the while loop and hardcode usrName and usrPW, I get the following:
127.0.0.1 - - [04/Dec/2017 08:09:34] "GET /cgi-bin/login.py HTTP/1.1" 200 -
127.0.0.1 - - [04/Dec/2017 08:09:39] code 404, message File not found
127.0.0.1 - - [04/Dec/2017 08:09:39] "GET /action_page.php?uname=a&psw=b HTTP/1.1" 404
However, the values get entered into the database, interestingly enough.
In the case that I leave the while loop running, it just loads indefinitely.