2016-04-26 19 views
0

Python Webアプリケーションのヘルプが必要です。私は、ユーザーが特定のフィールドを更新できるようにしたい(私はいくつかのフィールドを非表示にした)。私は以前の関数でupdateTrail()を呼び出しました。これはうまく表示されます。しかし、私が更新を提出するために行くとき、私の元の値は同じままです。私にとって奇妙なのは、私がフォームをデバッグすると、私が入れた更新された値が表示されるということです。Python WebアプリケーションでSQLデータベースを更新できません

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) 
+0

クエリに渡すパラメータより多くの '%s'プレースホルダがクエリにあります。 – alecxe

+0

@alecxeこれについては、これまで何度も私は気づいていませんでした!ありがとう、私はそれを修正しましたが、まだ更新されていません。 –

答えて

0

"name = form ['form']。値は" name = form ['name']。value "でなくてはなりませんか?

+0

AH私はこの笑いにどれくらいの間違いがあるか気付かなかった、それを指摘してくれてありがとう!しかし、それは私の問題を解決しませんでした。 –

+0

fetchwarningsメソッドで警告が生成されているかどうかを確認しようとしましたか?また、あなたが期待している値であることを確認するためにパラメータを印刷してみましたか?そしてもしそうなら、クエリにパラメータを挿入して、mysqlシェルで直接実行して、結果が期待どおりであることを確認してください。これらの手順は、可能性を絞り込むのに役立ちます。 – ghenghy

関連する問題