修改后的python代码如下:

import mysql.connector from flask import Flask, render_template, request, redirect, url_for, session import time

app = Flask(name)

Set the secret key for the session

app.secret_key = "secretkey"

Connect to MySQL database

mydb = mysql.connector.connect( host="localhost", user="root", password="123456", database="用户账号密码" )

Create a cursor object to interact with the database

mycursor = mydb.cursor()

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, systolic INT, diastolic INT, heartRate INT, sleepQuality INT, vegetables INT, fruits INT, meat INT, fish INT, dairy INT, grains INT, nuts INT, sugar INT, friedFood INT, processedFood INT)")

@app.route('/') def home(): # Check if the user is logged in if 'username' in session: return render_template('logined.html', username=session['username']) else: return render_template('index.html')

@app.route('/firstpage') def firstpage(): # Check if the user is logged in if 'username' in session: return render_template('logined.html', username=session['username']) else: return render_template('index.html')

@app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': email = request.form['email'] password = request.form['password']

    # Check if the username and password match the records in the database
    mycursor.execute("SELECT * FROM users WHERE email = %s AND password = %s", (email, password))
    user = mycursor.fetchone()

    if user:
        # Add the username to the session
        session['username'] = user[1]
        # Add a delay of 3 seconds before redirecting to the login success page
        time.sleep(3)
        return render_template('logined.html', username=user[1])
    else:
        return 'Invalid email or password'

return render_template('login.html')

@app.route('/register', methods=['GET', 'POST']) def register(): if request.method == 'POST': username = request.form['username'] email = request.form['email'] password = request.form['password']

    # Check if any of the fields are empty
    if not username or not email or not password:
        return 'All fields must be filled'

    # Check if the username exceeds the character limit
    if len(username) > 8:
        return 'Username cannot exceed 8 characters'

    # Check if the email exceeds the character limit
    if len(email) > 10:
        return 'Email cannot exceed 10 characters'

    # Check if the password exceeds the character limit
    if len(password) > 16:
        return 'Password cannot exceed 16 characters'

    # Check if the username already exists in the database
    mycursor.execute("SELECT * FROM users WHERE username = %s", (username,))
    user = mycursor.fetchone()

    # Check if the email already exists in the database
    mycursor.execute("SELECT * FROM users WHERE email = %s", (email,))
    email_exists = mycursor.fetchone()

    if user:
        return 'Username already exists'
    elif email_exists:
        return 'Email already exists'
    else:
        # Insert the new user into the database
        mycursor.execute("INSERT INTO users (username, email, password) VALUES (%s, %s, %s)",
                         (username, email, password))
        mydb.commit()
        # Add a delay of 3 seconds before redirecting to the registration success page
        time.sleep(3)
        return render_template('success.html', username=username)

return render_template('register.html')

@app.route('/logout') def logout(): # Remove the username from the session session.pop('username', None) # Add a delay of 3 seconds before redirecting to the home page time.sleep(3) return redirect(url_for('home'))

@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['name'] age = int(request.form['age']) height = int(request.form['height']) weight = int(request.form['weight']) systolic = int(request.form['systolic']) diastolic = int(request.form['diastolic']) heartRate = int(request.form['heartRate']) sleepQuality = int(request.form['sleepQuality']) vegetables = int(request.form['dietHabitsProportion']) fruits = int(request.form['dietHabitsProportion']) meat = int(request.form['dietHabitsProportion']) fish = int(request.form['dietHabitsProportion']) dairy = int(request.form['dietHabitsProportion']) grains = int(request.form['dietHabitsProportion']) nuts = int(request.form['dietHabitsProportion']) sugar = int(request.form['dietHabitsProportion']) friedFood = int(request.form['dietHabitsProportion']) processedFood = int(request.form['dietHabitsProportion'])

        # Update the user's information in the database
        mycursor.execute("UPDATE users SET name = %s, age = %s, height = %s, weight = %s, systolic = %s, diastolic = %s, heartRate = %s, sleepQuality = %s, vegetables
这是userinformhtml的信息:!DOCTYPE htmlhtmlhead titleUser Informationtitle style body font-family Arial sans-serif; background-image urlstaticimages5jpg; backg

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

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