#!/usr/bin/python
import sys
# define a function to print error messages
def error(msg):
print """ Error: %s""" % msg
print "\n"
sys.exit()
# script to show the test table in jdatabase jensen
print "Content-type: text/html\n"
print """
Insert request being processed for
Name: """
import cgi
import cgi #helpful module
form = cgi.FieldStorage() #form is now an object with the form data
# get and check all the form elements
name = form.getvalue("name") #a string
if name==None:
error("You must enter a name")
print name
name = name.replace("'","''") #GUARD against stray ' for use in SQL statements
idno = form.getvalue("id") #a string
if idno==None:
idno = 99999
elif not idno.isdigit():
error("id must be numeric!")
print "id: ",idno #still a string, but we know it's numeric
town = form.getvalue("town") #a string
if town==None:
town=""
print "town: ",town
town = town.replace("'","''") #GUARD against stray ' for use in SQL statements
# do the insert into the database
try:
import pg #module for postgresql database
con=pg.connect("jensen")
except:
print "not connected";
try:
result=con.query("insert into test values('%s',%s,'%s')"%(name,idno,town))
print "has been completed."
except pg.DatabaseError, message: #one error class, 'message' is
#the error string
print "Exception raised..."
print message #informative error message
except:
error("Something bad happened")
print "
Try again"
print ''