Advertisement
sm4rtn0bit4

Python-cgi for inserting entries in DB

Jul 29th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import cgi,cgitb
  3. import pymysql.cursors
  4. #
  5. #       Getting values from signup page fields
  6. #
  7. # Create instance of FieldStorage
  8. form = cgi.FieldStorage()
  9.  
  10. # Get data from fields
  11. username = form.getvalue('username')
  12. password  = form.getvalue('password')
  13. email = form.getvalue('email')
  14.  
  15. #
  16. #       Create connection config for DataBase
  17. #
  18. connection=pymysql.connect(
  19.         host='localhost',
  20.         user='test',
  21.         password='password',
  22.         db='test',
  23.         cursorclass=pymysql.cursors.DictCursor)
  24. #INSERT new user entries
  25. #
  26. try:
  27.     with connection.cursor() as cursor:
  28.         insert_query="INSERT INTO users (user_name,user_pass,user_email) VALUES (%s,%s,%s);"
  29.         cursor.execute(insert_query,(username,password,email))
  30.     #   For saving changes in DB
  31.     connection.commit()
  32.  
  33. finally:
  34.     connection.close()
  35. #showing variable values in browser for confirmation that it's taking values from field and it will be
  36. # submitted to DB also
  37. print("Content-type:text/html")
  38. print()
  39. print("Content-type:text/html\r\n\r\n")
  40. print("<html>")
  41. print("<head>")
  42. print("<title> Adding Credentials ... </title>")
  43. print("</head>")
  44. print ("<body>")
  45. print ("<h2>Hello %s  and your email is %s and pass is %s</h2>" % (username,email,password))
  46. print ("</body>")
  47. print ("</html>")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement