title: get id of a form field in python cgi
body: <p>I was Programming CGI With Python and found that CGI passes information to Python in the form of a class called FieldStorage. The main attribute of class FieldStorage which i came across is 'getvalue' to get data from form fields. Is it possible to get the id value of the field?</p>
  
  <p>Edit</p>
  
  <p>I was wondering if i can get some unique info about the form sending data. This is because, i have a form and 2 scripts.I the form, i put item details then a script1 takes that data and saves it in the db. i have another script2 which queries a saved item data, creates a form and populates the form with the data to enable editing the item and after the user edits, the data is updated back to the db.</p>
  
  <p>The updated back to the db is where the problem is. i wanted to use script 1 to do the update but in this case i wont do INSERT INTO ... i will do UPDATE Table1... Now, how do i know which request is for editing or updating. am doing this because im in a situation where the form has 70 fields and creating another script will just be copying script1 with addition of the update logic.
  This is what i wanted to do with the script</p>
  
  <p>script1.py</p>
  
  <pre><code>#!/usr/bin/env python
  
  import cgi
  import cgitb; cgitb.enable()     # for troubleshooting
  import MySQLdb
  
  form = cgi.FieldStorage()
  
  #there are 70 fields
  id = form.getfirst("id");
  error = form.getfirst("error");
  comment = form.getfirst("comment");
  product_name = form.getfirst("product_name");
  verified_product_name = form.getfirst("verified_product_name");
  url = form.getfirst("url");
  verified_url= form.getfirst("verified_url");
  .
  .
  .
  source = form.getfirst("source");
  
  print 'Content-type: text/html\r\n\r'
  print '&lt;html&gt;'
  print 'Update Successfull ! !'
  print '&lt;/html&gt;'
  
  
  db = MySQLdb.connect(host="localhost", user="root", passwd="", db="products")
  
  
  cursor = db.cursor()
  sql=""
  
  ### Check the request---How do i do this?
  
  if update: ##if the request is for updating
  
  sql = """UPDATE item SET (id=id, error=error, comment=comment, product_name=product_name, verified_product_name=verified_product_name, url=url, verified_url=verified_url, image_url=image_url,..., source=source)"""
  
  else: ##if the request is for inserting
  
  sql = """INSERT INTO item (id, error, comment, product_name, verified_product_name, url, verified_url, image_url, verified_image_url, brand, verified_brand, type, verified_type, price, min_price, max_price, verified_price,..., source) VALUES ('%s', '%s', "%s", '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',..., '%s')""" %(id, error, comment, product_name, verified_product_name, url, verified_url, image_url, verified_image_url, brand,..., source)
  
  try:
    cursor.execute(sql,(id, error, comment, product_name, verified_product_name, url, verified_url, image_url, verified_image_url, brand, verified_brand, type, verified_type, price, min_price, max_price, verified_price, measure_unit, ..., source))
  
     db.commit()
  except Exception as err:
  
  
     db.rollback()
  
     db.autocommit(True)
  cursor.execute(sql)
  cursor.fetchall()
  db.close()
  </code></pre>
  
  <p>Hope you got the my issue. Can i do this without creating another script to do the update seperately? </p>