我正在几个论坛中进行研究,但无法以简单的形式找到问题,以便在MySQL数据库中插入信息。你能找到错误吗?
由于
这是HTML表单代码(hello.html):
<html>
<body>
<form method="get" action="hello2.py">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Plate: <input type="text" name="plate">
<input type="submit" value="Submit">
</form>
</body>
Python代码(hello2.py):
#!/usr/bin/python
import MySQLdb
import cgi, cgitb
form = cgi.FieldStorage()
name = form.getvalue('name')
email = form.getvalue('email')
plate = form.getvalue('plate')
# Avoid script injection escaping the user input
name = cgi.escape(name)
email = cgi.escape(email)
plate = cgi.escape(plate)
db = MySQLdb.connect(host="mysql.example.com", port=3306, user="l", passwd="l", db="l")
cursor = db.cursor()
add_driver = ("INSERT INTO Users "
"(UsersName, UsersEmail, UsersPlate) "
"VALUES (%s, %s, %s)")
cursor.execute(add_driver, (name, email, plate))
db.commit()
cursor.close()
db.close()
print "Content-type: text/html\n\n"
print
print """\
<html>
<body>
<h2>Hello!</h2>
<ul>
</body>
</html>
"""
答案 0 :(得分:-1)
使用try来覆盖您的代码,除非看到错误(如果有的话)
try:
db = MySQLdb.connect(host="mysql.example.com", port=3306, user="l", passwd="l", db="l")
cursor = db.cursor()
add_driver = ("INSERT INTO Users (UsersName, UsersEmail, UsersPlate) VALUES (%s, %s, %s)")
cursor.execute(add_driver, (name, email, plate))
db.commit()
cursor.close()
db.close()
except Exception e:
print repr(e)