【问题标题】:Unable to update SQL database through python web app无法通过 python web 应用程序更新 SQL 数据库
【发布时间】:2016-08-20 18:53:32
【问题描述】:

在我的 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)

【问题讨论】:

  • 您在查询中的%s 占位符比您在查询中传递的参数多..
  • @alecxe 我已经在这方面工作了很长时间,我什至没有注意到!所以谢谢你,我修好了,但它仍然没有更新。

标签: python mysql web-applications


【解决方案1】:

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

【讨论】:

  • 啊我没有注意到我有多少错误哈哈,谢谢你指出这一点!但是,它并没有解决我的问题。
  • 您是否尝试查看是否有任何通过 fetchwarnings 方法生成的警告?另外,您是否尝试过打印出您的参数以确保它们是您期望的值?如果是,您是否将参数插入到查询中并直接在 mysql shell 中运行以确保结果符合您的预期?这些步骤将有助于缩小可能性。
猜你喜欢
  • 2019-07-02
  • 1970-01-01
  • 2015-09-01
  • 2011-04-07
  • 2016-06-12
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多