2

I have deleteuser.py where i need to delete one user, here is the code:

# Import modules for CGI handling
import MySQLdb
import cgi, cgitb

# Open database connection
db = MySQLdb.connect("localhost", "root", "", "moviedb" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
iduser = form.getvalue('iddelete')

# execute SQL query using execute() method.
try:
    cursor.execute("""DELETE FROM user WHERE
         ID = '%s'""",(iduser))
    # Commit your changes in the database
    db.commit()
except:
    db.rollback()

# disconnect from server
db.close()

print "Content-Type: text/plain;charset=utf-8"
print

I have no error but it doesn`t work. The database is still the same.

Thank you

4
  • DOn't put code in comments, edit the question. Commented May 4, 2016 at 19:45
  • 1
    You're carefully hiding all possible errors with that catch-all except block. Remove that so you can see what is actually going wrong. Never, ever, blindly catch and swallow exceptions. Commented May 4, 2016 at 19:50
  • i have removed try-catch, now i have this error: End of script output before headers Commented May 4, 2016 at 20:16
  • thank you, that helped me a lot. now i have the answer posted Commented May 4, 2016 at 20:41

1 Answer 1

1

here is the answer:

#!/Python27/python
# -*- coding: UTF-8 -*-


# Import modules for CGI handling
import MySQLdb
import cgi, cgitb

# Open database connection
db = MySQLdb.connect("localhost", "root", "", "moviedb" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
iduser = form.getvalue('iddelete')

# execute SQL query using execute() method.
query = "delete from user where id = '%s' " % iduser
cursor.execute(query)
    # Commit your changes in the database
db.commit()

# disconnect from server
db.close()

print "Content-Type: text/plain;charset=utf-8"
print
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for posting your answer. However, don't forget to be specific about what the error was and exactly how you resolved it so that other than benefit from your answer in the future.
Now, imagine what happens if iduser obtained from cgi is NULL; DROP TABLE user.

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.