need help with cgi script

Akhar akhar at videotron.ca
Tue Mar 7 02:03:17 CET 2000


I made  a script (enclosed) to be able to do some database administration
and it quits after the first execpt (RaiseError...) ! why?
Thank for any help
Akhar
#!/usr/local/bin/python
import cgi
import MySQLdb
import string
I = "Content-type: text/html"
J = "<HTML><TITLE>ADMIN SITE </TITLE><BODY>"
print I
print
print J
try:
 try:
  form = cgi.FieldStorage()
  NField = form["nom"]
  SField = form["serial"]
  TField = form["type"]
  PField = form["prix"]
  DField = form["descrip"]
  QField = form["qte"]
  PHField = form["photo"]
  CField = form["com"]
  print "step 1"
 except (KeyError, TypeError):
   q = cgi.print_form(form)
   print q
   print "<H1>ay shit</H1>"
   raise SystemExit
 try:
  N = NField.value
  S = SField.value
  T = TField.value
  P = PField.value
  D = DField.value
  Q = QField.value
  PH = PHField.value
  C = CField.value
  db = MySQLdb.connect(host='localhost', user='addabas', db='test')
  db
  cursor = db.cursor()
  if form.has_key("name"):
   if form["name"].value=='Cherche':
    print '<FORM METHOD="POST" ACTION="/cgi-bin/find.py">'
    ASK = 'select * from item where nom like "%'+ N + '%" or serial like "%'
+ S + '%" or type like "%' + T + '%" or prix like "%' + P + '%" or descrip
like "%' + D + '%" or  qte like "%' + Q + '%" photo like "%' + PH +'%" or
com like "%' + C + '%"'
    cursor.execute(ASK)
    results = cursor.fetchall()
    tot = len(results)
    entries = []
    print I
    print J
    if tot < 1:
     print "ay shity tyti ban bang"
    else:
     for record in range(tot):
      entry = {}
      entry["nom"] = results[record][0]
      entry["serial"] = results[record][1]
      entry["type"] = results[record][2]
      entry["prix"] =results[record][3]
      entry["descrip"] = results[record][4]
      entry["qte"] = results[record][5]
      entry["photo"] = results[record][6]
      entry["com"] = results[record][7]
      entries.append(entry)
    for entry in entries:
     print """<P>Nom Produit:
     <TEXTAREA NAME="text" ROWS="20" COLS="100">"""
     print entry["nom"]
     print "</TEXTAREA></P>"
     q = '<P>Type: <INPUT TYPE="TEXT" NAME="type" VALUE="' + entry["type"] +
"></P>"
     ww = '<P>Prix: <INPUT TYPE="TEXT" NAME="prix"' + entry["prix"] +
"></P>"
     e = '<P>Description: <INPUT TYPE="TEXT" NAME="descrip" VALUE="' +
entry["descrip"] + "></P>"
     r = '<P>Quantité en stock: <INPUT TYPE="TEXT" NAME="qte"' +
entry["qte"] + "></P>"
     t = '<P>Photo: <INPUT TYPE="TEXT" NAME="photo"' + entry["photo"] +
"></P>"
     y = '<P>Com: <INPUT TYPE="TEXT" NAME="com"' + entry["com"] + "></P>"
     print q
     print ww
     print e
     print r
     print t
     print y
     print """<P><INPUT TYPE="SUBMIT" VALUE="modif"></P>
      <P><INPUT TYPE="SUBMIT" VALUE="efface"></P>
      <P><INPUT TYPE="SUBMIT" VALUE="ajout"></P>
      <P><INPUT TYPE="SUBMIT" VALUE="search"></P>
      </FORM>"""
   if form["name"].value=='Modifier':
    TXT =  "'" + N + "', '" + S + "', '" + T + "', '" + P + "', '" + D + "',
'" +Q + "', '" +PH + "', '" + C + "'"
    COM = "replace into item values(" + TXT + ")"
    GIVE = cursor.execute(COM)
    GIVE
    print "<PRE> Name of prod </PRE>"
    print "La base de données à été modifié selon vos spécifications!!! ;0)"
    print TXT
    print """(html of theform page)"""
   if form["name"].value=='Ajout':
    TXT =  "'" + N + "', '" + S + "', '" + T + "', '" + P + "', '" + D + "',
'" +Q + "', '" +PH + "', '" + C + "'"
    COM = "insert into item values(" + TXT + ")"
    GIVE = cursor.execute(COM)
    GIVE
    print "<PRE> Name of prod </PRE>"
    print "Les données ont été ajouté à la base de données selon vos
spécifications!!! ;0)"
    print TXT
    print """(html of theform page)"""
   if form["name"].value=='Effacer':
    TXT =  "'" + N + "', '" + S + "', '" + T + "', '" + P + "', '" + D + "',
'" +Q + "', '" +PH + "', '" + C + "'"
    COM = "insert into item values(" + TXT + ")"
    GIVE = cursor.execute(COM)
    GIVE
    print "<PRE> Name of prod </PRE>"
    print "Les données ont été effacées de la base de données selon vos
spécifications!!! ;0)"
    print TXT
    print """(html of theform page)"""
   else:
    print "nay! nay! nay!"
  else:
   print "/BOD/HTML"
 finally:
  print "done"
finally:
 print "</BODY></HTML>"
this is the HTML:
<HTML>
<HEAD>
  <META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Win">
  <TITLE>admi pages</TITLE>
</HEAD>
<BODY LINK="#000099" VLINK="#ff0000">

<FORM ACTION="/cgi-bin/admin.py" ENCTYPE="x-www-form-urlencoded"
METHOD="POST">
<P>&nbsp;</P>

<P><INPUT NAME="name" TYPE="reset" VALUE="Reset"><INPUT NAME="ajout"
TYPE="submit" VALUE="Ajout"><INPUT NAME="modif" TYPE="submit"
VALUE="Modifier"><INPUT NAME="efface" TYPE="submit" VALUE="Effacer"><INPUT
NAME="search" TYPE="submit" VALUE="Cherche"></P>

<P>Nom du produit <INPUT NAME="nom" TYPE="text" SIZE="25" MAXLENGTH="230">
Num&eacute;ro de s&eacute;rie<INPUT NAME="serial" TYPE="text"
SIZE="25" MAXLENGTH="230"></P>

<P>Type/Gamme du produit<INPUT NAME="type" TYPE="text" SIZE="25"
MAXLENGTH="230"> Prix<INPUT NAME="prix" TYPE="text" SIZE="25"
MAXLENGTH="230"></P>

<P>Description<TEXTAREA NAME="decrip" ROWS="5" COLS="67" 0
></TEXTAREA></P>

<P>Quantit&eacute;<INPUT NAME="qte" TYPE="text" SIZE="25"
MAXLENGTH="230">Photo(s)<INPUT
NAME="photo" TYPE="text" SIZE="25" MAXLENGTH="230"></P>

<P>Commentaire(s)<TEXTAREA NAME="com" ROWS="5" COLS="65"
></TEXTAREA></P>

<P>&nbsp;</FORM>

</BODY>
</HTML>






More information about the Python-list mailing list