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 に答える