Saturday, 15 January 2011

get id of a form field in python cgi -



get id of a form field in python cgi -

i programming cgi python , found cgi passes info python in form of class called fieldstorage. main attribute of class fieldstorage came across 'getvalue' info form fields. possible id value of field?

edit

i wondering if can unique info form sending data. because, have form , 2 scripts.i form, set item details script1 takes info , saves in db. have script2 queries saved item data, creates form , populates form info enable editing item , after user edits, info updated db.

the updated db problem is. wanted utilize script 1 update in case wont insert ... update table1... now, how know request editing or updating. doing because im in situation form has 70 fields , creating script copying script1 add-on of update logic. wanted script

script1.py

#!/usr/bin/env python import cgi import cgitb; cgitb.enable() # troubleshooting import mysqldb form = cgi.fieldstorage() #there 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 '<html>' print 'update successfull ! !' print '</html>' db = mysqldb.connect(host="localhost", user="root", passwd="", db="products") cursor = db.cursor() sql="" ### check request---how do this? if update: ##if request 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 request inserting sql = """insert 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 err: db.rollback() db.autocommit(true) cursor.execute(sql) cursor.fetchall() db.close()

hope got issue. can without creating script update seperately?

python cgi

No comments:

Post a Comment