przez MitchelBraybh 15 Maj 2023, 06:15
Hi everyone, I appreciate your help. I'm currently working on a school website and I need assistance with logging a user's logout time in my database. Here's the code I've written so far:
import datetime
import mysql.connector
# Connect to the database
db = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="yourdatabase"
)
# Get the current time
logout_time = datetime.datetime.now()
# Retrieve the user's ID or any unique identifier
user_id = "your_user_id"
# Prepare the SQL query
query = "UPDATE Users SET LOdatetime = %s WHERE user_id = %s"
# Execute the query
cursor = db.cursor()
cursor.execute(query, (logout_time, user_id))
# Commit the changes and close the connection
db.commit()
db.close()
Please make sure to replace "localhost," "username," "password," "yourdatabase," and "your_user_id" with the appropriate values for your setup.
With this code, the user's logout time will be recorded in the "LOdatetime" column of the "Users" table in your database. Let me know if you have any further questions or issues. Thank you!