Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Tkinter创建登录页面--登录验证过程未按预期工作_Python_Python 3.x_Database_Authentication_Tkinter - Fatal编程技术网

Python 使用Tkinter创建登录页面--登录验证过程未按预期工作

Python 使用Tkinter创建登录页面--登录验证过程未按预期工作,python,python-3.x,database,authentication,tkinter,Python,Python 3.x,Database,Authentication,Tkinter,我正在尝试使用连接到数据库的Tkinter创建登录页面。用户的详细信息应在注册后存储在数据库中,然后用于在登录时验证用户。注册部分工作正常,但是,登录验证过程中似乎有错误,因为即使输入了正确的凭据,程序也会不断输出“未找到用户”。 如果您能深入了解为什么会发生这种情况,我们将不胜感激 db_cursor = db_connection.cursor() db_cursor.execute("CREATE DATABASE IF NOT EXISTS userd

我正在尝试使用连接到数据库的Tkinter创建登录页面。用户的详细信息应在注册后存储在数据库中,然后用于在登录时验证用户。注册部分工作正常,但是,登录验证过程中似乎有错误,因为即使输入了正确的凭据,程序也会不断输出“未找到用户”。 如果您能深入了解为什么会发生这种情况,我们将不胜感激

         db_cursor = db_connection.cursor()
         db_cursor.execute("CREATE DATABASE IF NOT EXISTS userdetails")
         db_cursor.execute("SHOW DATABASES")
        for db in db_cursor:
          print(db)
        db_cursor.execute("CREATE TABLE IF NOT EXISTS userdetails.users(userid INT(100) 
        AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255))")
        db_cursor.execute("SHOW TABLES IN userdetails")
        for table in db_cursor:
          print(table)

 class LoginPage:
   def __init__(self):
      self.userID = 1
      self.mainpage()

def mainpage(self):
    global mainpage
    mainpage = Tk()
    mainpage.geometry("600x600")
    mainpage.title("Login Page")
    mainpage.configure(background="thistle1")
    Label(mainpage, text="Please login or register for a new account", bg="plum2", width="300", height="2", font=("Calibri", 13)).pack()
    Button(mainpage, text="Login",bg="SkyBlue1", height="2", width="30", command=self.login).pack()
    Button(mainpage, text="Register",bg="SkyBlue1", height="2", width="30", command=self.register).pack()
    mainpage.mainloop()

def register(self):
    global register
    global username
    global password
    Label(mainpage, text="Fill in the boxes below to register as a new user", bg="plum2").pack()
    username = str
    username_label = Label(mainpage, text="Username:  ", bg="SkyBlue1").pack()
    usernameRegister_entry = Entry(mainpage, textvariable=username).pack()
    password = str
    password_label = Label(mainpage, text="Password:  ",bg="SkyBlue1").pack()
    passwordRegister_entry = Entry(mainpage, textvariable=password, show='*').pack()
    Button(mainpage, text="Register", width=10, height=1, bg="plum2", command = self.new_user).pack()

def new_user(self):
    self.userID += 1
    userdetails_sql_query = "INSERT INTO userdetails.users VALUES(userID,'username','password')"
    db_cursor.execute(userdetails_sql_query)
    db_connection.commit()
    print(db_cursor.rowcount, "Record Inserted")
    Label(mainpage,text="Registration Success", bg="plum2", font=("calibri", 11)).pack()

def login(self):
    global usernameCheck
    global passwordCheck
    global login
    Label(mainpage, text="Fill in the boxes below to login", bg="plum2").pack()
    usernameCheck = str
    Label(mainpage, text="Username: ", bg="SkyBlue1").pack()
    usernameLogin_entry = Entry(mainpage, textvariable=usernameCheck).pack()
    passwordCheck = str
    Label(mainpage, text="Password: ", bg="SkyBlue1").pack()
    passwordLogin_entry = Entry(mainpage, textvariable=passwordCheck, show= '*').pack()
    Button(mainpage, text="Login", width=10, height=1, bg="plum2", command=self.login_verification).pack()

def login_verification(self):
    sql_select_Query = "select * from userdetails.users"
    db_cursor.execute(sql_select_Query)
    records = db_cursor.fetchall()
    if usernameCheck in records:
        if passwordCheck in records:
            self.login_success()
        else:
            self.incorrect_password()
    else:
        self.incorrect_user()

def login_success(self):
    global login_success
    Label(mainpage, text="You have logged in successfully!", bg="plum2").pack()
    return self.login_success

def incorrect_password(self):
    Label(mainpage, text="Invalid Password ", bg="plum2").pack()

def incorrect_user(self):
    Label(mainpage, text="User Not Found", bg="plum2").pack()
这是不对的,

    if usernameCheck in records:
        if passwordCheck in records:
            self.login_success()
        else:
            self.incorrect_password()
    else:
        self.incorrect_user()
您可以迭代记录,并为每个记录检查用户和密码,但即使如此,也不是这样做的方法

您应该构建一个参数化查询,检查用户和密码,然后检查查询是否返回记录

差不多

query = "select * from userdetails.users where user=? and password=?"
    db_cursor.execute(query, (user, password))
    record = db_cursor.fetchall()
    if record:
        self.login_success()
    else:
        self.incorrect_user()