gpt4 book ai didi

python - 无法通过 python Web 应用程序更新 SQL 数据库

转载 作者:行者123 更新时间:2023-11-29 21:05:17 26 4
gpt4 key购买 nike

需要有关我的 Python Web 应用程序的帮助。我希望用户能够更新某些字段(我隐藏了一些字段)。我在之前的函数中调用了 updateTrail() ,它显示得很好。但是,当我提交更新时,我的原始值保持不变。对我来说奇怪的是,当我调试表单时,会显示我输入的更新值。

 def getUpdate(activities, dogs, restrooms, freepark, open, contact, name):
"""
Middleware function that will update objects in the database.
"""
# connect to db
conn, cursor = getConnectionAndCursor()

# prepare SQL
sql = """
UPDATE hiking
SET town=%s, region=%s, activities=%s, dogs=%s, restrooms=%s, freepark=%s,open=%s, contact=%s, lat=%s, lng=%s
WHERE name=%s
"""

parameters=(activities, dogs, restrooms, freepark, open, contact, name)
# run the SQL
cursor.execute(sql, parameters)

# fetch the results
data = cursor.fetchall()

print "<mark> %d row(s) were inserted.</mark>" % cursor.rowcount

# clean up
cursor.commit() #commits the changes to the database
cursor.close()
conn.close()

return data
################################################################################
def updateTrail(name, town, region, activities, dogs, restrooms, freepark, open, contact, lat, lng):
"""
Allows the user to update a trail of their choosing.
"""
print'''
<center>
<form>
<input type="hidden" name="name" value="%s">

<input type="hidden" name="town" value="%s">

<input type="hidden" name="region" value="%s">

<font color="white"><b>Update Activities</b></font>
<input type="text" name="activities" value="%s">

<font color="white"><b>Are Dogs Allowed?</b></font>
<input type="text" name="dogs" value="%s">

<font color="white"><b>Are Restrooms Available?</b></font>
<input type="text" name="restrooms" value="%s">

<font color="white"><b>Is there free parking?</b></font>
<input type="text" name="freepark" value="%s">

<br>

<font color="white"><b>When is the trail open?</b></font>
<input type="text" name="open" value="%s">

<font color="white"><b>Update Phone Number</b></font>
<input type="text" name="contact" value="%s">

<input type="hidden" name="lat" value="%s">

<input type="hidden" name="lng" value="%s">

<br>

<input type="submit" name="updateTrail" value="Update Trail Information">
</form>
</center>

''' % (name, town, region, activities, dogs, restrooms, freepark, open, contact, lat, lng)

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

if __name__ == "__main__":

# get form field data
form = cgi.FieldStorage()
debugFormData(form)

elif 'updateTrail' in form:
#unpack into python variable
name = form['form'].value
town = form['town'].value
region = form['region'].value
activities = form['activities'].value
dogs = form['dogs'].value
restrooms = form['restrooms'].value
freepark = form['freepark'].value
open = form['open'].value
contact = form['contact'].value
lat = form['lat'].value
lng = form['lng'].value

#update the trail's information to the database
getUpdate(activities, dogs, restrooms, freepark, open, contact)

最佳答案

“name = form['form'].value”不应该是“name = form['name'].value”吗?

关于python - 无法通过 python Web 应用程序更新 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36873491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com