1

我在 html 文档中创建了一个表单来从用户那里获取数据。我在 mysql 中创建了一个数据库。我正在使用 python 将数据库连接到表单,以便用户输入填充数据库。但它不工作。提交值后,我在浏览器页面上打印了以下代码,并且没有值从表单中插入到数据库中。你能指出代码中的错误/任何修改吗?

import cgitb;cgitb.enable()
import MySQLdb
import cgi
import sys

form=cgi.FieldStorage()
Text0=form.getValue('Text0')
Text1=form.getValue('Text1')
Text2=form.getValue('Text2')
Text3=form.getValue('Text3')
Text4=form.getValue('Text4')

# Open database connection
dbc = MySQLdb.connect("127.0.0.1","root","sam143","project" )

# prepare a cursor object using cursor() method
cursor = dbc.cursor()

# Prepare SQL query to INSERT a record into the database.

sql = """INSERT INTO entries(slno, \
       dat1, dat2, dat3, dat4, dat5) \
       VALUES ('%d', '%d', '%d', '%d', '%d', '%d' )""" % \
       (l[0], l[1], l[2], l[3], l[4])

try:
   # Execute the SQL command
   cursor.execute(sql)
   sql = "SELECT * FROM entries"
   cursor.execute(sql)
   print cursor.fetchall()
   # Commit your changes in the database
   dbc.commit()

except:
   # Rollback in case there is any error
   dbc.rollback()

# disconnect from server
dbc.close()
4

1 回答 1

1
>>> form=cgi.FieldStorage()
>>> dir(form)
['FieldStorageClass', '_FieldStorage__write', '__contains__', '__doc__', '__getattr__', '__getitem__', '__init__', '__iter__', '__len__', '__module__', '__nonzero__', '__repr__', 'bufsize', 'disposition', 'disposition_options', 'done', 'fi
le', 'filename', 'fp', 'getfirst', 'getlist', 'getvalue', 'has_key', 'headers', 'innerboundary', 'keep_blank_values', 'keys', 'length', 'list', 'make_file', 'name', 'outerboundary', 'qs_on_post', 'read_binary', 'read_lines', 'read_lines_to
_eof', 'read_lines_to_outerboundary', 'read_multi', 'read_single', 'read_urlencoded', 'skip_lines', 'strict_parsing', 'type', 'type_options']
>>> 

form.getvalue 不是 form.getValue

于 2013-02-27T18:24:28.447 回答