python写cgi对mysql数据库纪录进行增,删,改操作

python写cgi对mysql数据库纪录进行增,删,改操作


图示里面是对mysql数据库进行增,删,改的cgi页面,上面是增加mysql纪录,已经完成。最下面是我打印的mysql数据库纪录,同时我想实现update,或者delete,未完成,比如我想更改某条纪录,怎样才能把当前纪录找到传给下一个cgi页面,然后我进行sql的操作?
以下是代码:
#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable(display=0,logdir="/tmp")
import MySQLdb


form=cgi.FieldStorage()
name=form.getvalue('user')
email=form.getvalue('email')
if form.getvalue('sex'):
  sex=form.getvalue('sex')
else:
  sex="Not set"
password=form.getvalue('password')

db=MySQLdb.connect("localhost","root","quake3","pytest")
cursor=db.cursor()
sql1="select name from sys_user"
sql2="insert ignore into sys_user(name,sex,password,email) values('%s','%s','%s','%s')" %(name,sex,password,email)
sql3="select * from sys_user"
cursor.execute(sql1)
namelist=cursor.fetchall()
nameflag=2
if form.has_key("user"):
  for n in namelist:
    if not n[0]==name:
      nameflag=1
    else:
      nameflag=0
      break
if nameflag==1:
  cursor.execute(sql2)
cursor.execute(sql3)
records=cursor.fetchall()
db.close()

# Generate the HTML response

#######################################################

print """content-type: text/html

<html><head><title>MySQL and Python on the Web</title></head>
<body bgcolor=#FFCCFF><h1>User Registration</h1>
User registration<hr>
<b>Step 1: </b><br>
"""


print """
<hr>
<form method=POST>Full name: <input type=text name=user><br>
Sex:<input type=radio name=sex value="Male">Male
    <input type=radio name=sex value="Female">Female<br>
Password:<input type=password name=password><br>
Email address:<input type=text name=email><br>
<input type=submit value="Submit"><input type=reset value="Reset">
</form>
<hr>
"""
if not form.has_key("user"):
#if name ==None:
  print "Notice:You have to type your full name<hr>"
if nameflag==0:
  print "Name is already taken, please choose another name"
print "<br>Your name is  %s" %name
print "<br>Your sex is %s" %sex
print "<br>Your password is %s" %password
print "<br>Your Email is %s <br>" %email
print "<hr>All the registration information:<hr>"
print """<table border="1">"""
for row in records:
  print "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>" %(row[0],row[1],row[2],row[3])
  print """<td><input type=button onClick="location.href='update.py'" value="Update"></td>"""
  print """<td><input type=button onClick="location.href='delete.py'" value="Delete"></td></tr>"""
print "</table>"
print """
</body></html>
"""
怎么插入图片?  我插入的图片链接看不到图片
cgi和mysql有啥关系?