【发布时间】:2017-03-29 13:44:29
【问题描述】:
http://cs1.ucc.ie/~adc2/cgi-bin/lab7/index.html 您可以通过在任何一个框中输入任何内容来自己检查错误,不必全部,任何帮助都会很棒 ,我会在此之后发送代码
from cgitb import enable
enable()
from cgi import FieldStorage,escape
print('Content-Type: text/html')
print()
actor=''
genre=''
theyear=''
director=''
mood=''
result=''
form_data= FieldStorage()
if len(form_data) != 0:
try:
actor=escape(form_data.getfirst('actor'))
genre=escape(form_data.getfirst('genre'))
theyear=escape(form_data.getfirst('theyear'))
director=escape(form_data.getfirst('director'))
mood= escape(form_data.getfirst('mood'))
connection = db.connect('####', '###', '####', '###')
cursor = connection.cursor(db.cursors.DictCursor)
cursor.execute("""SELECT title
FROM films
WHERE (actor = '%s')
OR (actor='%s' AND genre='%s')
OR (actor='%s' AND genre='%s' AND theyear='%i')
OR (actor='%s' AND genre='%s' AND theyear='%i' AND director='%s')
OR (actor='%s' AND genre='%s' AND theyear='%i' AND director='%s' AND mood='%s') % (actor, actor,genre, actor,genre,theyear, actor,genre,theyear,director,actor,genre,theyear,director,mood))
""")
result = """<table>
<tr><th>Your movie!</th></tr>
<tr><th></th></tr>"""
for row in cursor.fetchall():
result+= '<tr><td>%s</td></tr>' ,(row['title'])
result+= '</table>'
cursor.close()
connection.close()
except db.Error:
result = '<p>Sorry! We are currently experiencing technical difficulties.</p>'
【问题讨论】:
-
嗯,关于错误还有什么不清楚的地方:你打电话给
replace,所以你可能假设它是str,但它是None。所以我认为输入无法通过 API 正确运行。 -
好的,谢谢,我已经添加了代码
-
我没有在该代码中看到对
str.replace()的调用。请在您的问题中提供完整的回溯。 -
这是因为你没有提供 genre 在这种情况下它是
None。因此,您应该将form_data.getfirst('genre')替换为form_data.getfirst('genre') or ''(并使用所有类似的行) -
result+= '<tr><td>%s</td></tr>' ,(row['title'])你的意思是result+= '<tr><td>%s</td></tr>' % (row['title'])(将逗号替换为percent)?