[DB-SIG] warning problem

Andy Dustman andy@dustman.net
18 Aug 2002 13:34:55 -0400


On Fri, 2002-08-16 at 09:39, Watt III, Glenn wrote:
> Ok heres my problem i am running a cgi in which i am updating a database
> here it is

Here's a shorter (untested) version, which may be useful for other
people on this list (or perhaps someone reading the archives before
asking a question, like that'll happen).

#!/usr/local/bin/python

import cgi
import sys
import MySQLdb

try:
    query = cgi.FieldStorage()
except:
    print"Please run via CGI... Thank you"
    sys.exit(0)

connection = MySQLdb.Connect(host="localhost",user="site",passwd="s1te411",db="backbone")
cursor = connection.cursor()

if query.has_key("accept"):
    header = "Content-type: text/html\n"
    print header
    print """
       <html>
        <head>
        <title>Hamburgerstand.com</title>
        </head>
        <body>"""
else:
    print "Well that was kind of useless you didn't even decide whether or not to accept"
    sys.exit(0)

columns = 'firstname lastname title email web city state country ' \
          'papertitle abstract cot'.split()
changes = {}

if query['accept'].value == "Yes":
    for c in columns:
        if query.has_key(c):
            changes[c] = query[c].value
    placeholders = [ "%s=%%(%s)s" % (k,k) for k in changes.keys() ]
    stmt = "UPDATE newspapers SET %s" % placeholders
else:
    stmt = 'delete from newpapers where firstname=%s'
    changes['refname'] = query["refname"].value
print stmt % MySQLdb.literal(changes)
cursor.execute(stmt, changes)
sys.exit(0)

-- 
Andy Dustman         PGP: 0x930B8AB6
    @       .net     http://dustman.net/andy
"Cogito, ergo sum." -- Rene Descartes
"I yam what I yam and that's all what I yam." -- Popeye