0

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:

  1. 404 with unable to find file

  2. 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.

7
  • Complete traceback of your script failing would be nice. Commented Dec 4, 2017 at 15:06
  • @BoboDarph How do I do this? (I will gladly make this easier to diagnose however I can). Commented Dec 4, 2017 at 15:06
  • Hardcode the values you need to something inside the script and run it like your server would. Commented Dec 4, 2017 at 15:07
  • @BoboDarph Done. Commented Dec 4, 2017 at 15:13
  • I was thinking more along the line of this: stackoverflow.com/questions/7956696/debugging-cgi-python. But if your hardcoded data gets pushed to the DB, the problem lies somewhere outside your script most likely. Commented Dec 4, 2017 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.