User Profile Update

User Profile Update



    <label for="age">Age:</label>
    <input type="number" id="age" name="age" value="{{ user[5] }}"><br><br>

    <label for="height">Height:</label>
    <input type="number" id="height" name="height" value="{{ user[6] }}"><br><br>

    <label for="weight">Weight:</label>
    <input type="number" id="weight" name="weight" value="{{ user[7] }}"><br><br>

    <label for="newpassword">New Password:</label>
    <input type="password" id="newpassword" name="newpassword"><br><br>

    <input type="submit" value="Update">
</form>
# Create a table for storing user registration information mycursor.execute( "CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(12), email VARCHAR(255), password VARCHAR(16), name VARCHAR(50), age INT, height INT, weight INT)") @app.route('/userinform', methods=['GET', 'POST']) def userinform(): # Check if the user is logged in if 'username' in session: if request.method == 'POST': name = request.form.get('name') age = request.form.get('age') height = request.form.get('height') weight = request.form.get('weight') newpassword = request.form.get('newpassword')
        # Update the user's information in the database
        if name:
            mycursor.execute("UPDATE users SET name = %s WHERE username = %s",
                             (name, session['username']))
        if age:
            try:
                age = int(age)
                mycursor.execute("UPDATE users SET age = %s WHERE username = %s",
                                 (age, session['username']))
            except ValueError:
                return 'Age must be a valid integer'
        if height:
            try:
                height = int(height)
                mycursor.execute("UPDATE users SET height = %s WHERE username = %s",
                                 (height, session['username']))
            except ValueError:
                return 'Height must be a valid integer'
        if weight:
            try:
                weight = int(weight)
                mycursor.execute("UPDATE users SET weight = %s WHERE username = %s",
                                 (weight, session['username']))
            except ValueError:
                return 'Weight must be a valid integer'

        # Update the user's password if a new password is provided
        if newpassword:
            mycursor.execute("UPDATE users SET password = %s WHERE username = %s",
                             (newpassword, session['username']))

        mydb.commit()

        # Get the updated user's information from the database
        mycursor.execute("SELECT * FROM users WHERE username = %s", (session['username'],))
        user = mycursor.fetchone()

        # Pass the user's information to the template
        return render_template('userinform.html', user=user)

    else:
        # Get the user's information from the database
        mycursor.execute("SELECT * FROM users WHERE username = %s", (session['username'],))
        user = mycursor.fetchone()

        # Pass the user's information to the template
        return render_template('userinform.html', user=user)

else:
    return redirect(url_for('home'))

原文地址: https://www.cveoy.top/t/topic/pHID 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录