Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem verifying a login. For example, I have 3 records in the database and only 1 user and password are valid, the other two are 2, it always throws me to the "ELIF" that the data is incorrect.

What can be due to that?

I have
user: 1
password: 1

user: 2
password: 2

user: 3
password: 3

Well, of these 3 users, only user 3 fits me well, the other 2 users send me to the "elif" of incorrect data, when everything is fine.

What I have tried:

Python
self.connection2 = connect_to_database()
self.cursor2 = self.connection2.cursor()
# Ejemplo: Ejecutar una consulta para obtener datos
self.cursor2.execute("SELECT nombre,password FROM usuarios")
self.data2 = self.cursor2.fetchall()
#messagebox.showwarning("Advertencia", self.data2)
# Cerrar el cursor y la conexión

for self.fila in self.data2:
    self.v_email = self.fila[0]
    self.v_password = self.fila[1]

self.cursor2.close()
self.connection2.close()

if self.entry_email.get() == "" or self.entry_password.get() == "":
    self.dialogo = tk.Toplevel()
    self.dialogo.title("ERROR")
    self.dialogo.geometry("300x50")
    self.dialogo.resizable(0,0)

    self.wtotal2 = self.dialogo.winfo_screenwidth()
    self.htotal2 = self.dialogo.winfo_screenheight()

    self.wventana2 = 300
    self.hventana2 = 100

    self.pwidth2 = round(self.wtotal2/2-self.wventana2/2)
    self.pheight2 = round(self.htotal2/2-self.hventana2/2)

    self.dialogo.geometry(str(self.wventana2)+"x"+str(self.hventana2)+"+"+str(self.pwidth2)+"+"+str(self.pheight2))
    self.etiqueta = tk.Label(self.dialogo, text="Rellena todos los campos.", padx=10, pady=10)
    self.etiqueta.pack()

    self.image_boton_vale = Image.open("src/boton_vale.png")
    self.photo_boton_vale = ImageTk.PhotoImage(self.image_boton_vale)

    self.boton_vale = tk.Button(self.dialogo, image=self.photo_boton_vale, command=self.cerrar_dialogo_vale)
    self.boton_vale.pack(pady=5)
    self.boton_vale.place(x=86, y=50, width=128, height=31)

elif self.entry_email.get() == self.v_email and self.entry_password.get() == self.v_password:
    self.app.root.destroy()
    self.ventana_menu = Menus(self)
else:
    self.app.root.destroy()
    messagebox.showwarning("Advertencia", "Los datos introducidos son incorrectos.")
Posted
Updated 10-Aug-23 6:10am
v2

Quote:
What could cause this error?
Indentation in Python is significant: it controls what is and isn't in a block of code.
All the contiguous code indented by the same amount (or more) is part of teh same code block:
Python
while foo == bar:
    Part of loop
    if (foo + bar == foobar:
        Still part of loop
Not part of loop -0 executed once the loop exits.
So your code:
Python
for self.fila in self.data2:
    self.v_email = self.fila[0]
    self.v_password = self.fila[1]

self.cursor2.close()
Only contains two lines inside the loop, and will always leave the same value in the email and password fields - the last one read from the DB.
Subsequent code will never get a chance to look at the other users data.

You need to think about exactly what you are doing here, and do some more processing inside your loop to decide which row to exit on.

Additionally, you should never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] -= it's C# based, but Python has similar methods.

And remember: if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 
Comments
Tomas Sanchez Garcia 10-Aug-23 11:37am    
Hi, thank you very much for your answer. I have tried testing this way with more statements and within the FOR loop, but it keeps giving me the same error, only one user reads me and not the others.

Regarding security, thanks for the information, this is only for the purpose of learning nothing professional for the public.

def validar(self):

if self.entry_email.get() == "" or self.entry_password.get() == "":
self.dialogo = tk.Toplevel()
self.dialogo.title("ERROR")
self.dialogo.geometry("300x50")
self.dialogo.resizable(0,0)

self.wtotal2 = self.dialogo.winfo_screenwidth()
self.htotal2 = self.dialogo.winfo_screenheight()

self.wventana2 = 300
self.hventana2 = 100

self.pwidth2 = round(self.wtotal2/2-self.wventana2/2)
self.pheight2 = round(self.htotal2/2-self.hventana2/2)

self.dialogo.geometry(str(self.wventana2)+"x"+str(self.hventana2)+"+"+str(self.pwidth2)+"+"+str(self.pheight2))
self.etiqueta = tk.Label(self.dialogo, text="Rellena todos los campos.", padx=10, pady=10)
self.etiqueta.pack()

self.image_boton_vale = Image.open("src/boton_vale.png")
self.photo_boton_vale = ImageTk.PhotoImage(self.image_boton_vale)

self.boton_vale = tk.Button(self.dialogo, image=self.photo_boton_vale, command=self.cerrar_dialogo_vale)
self.boton_vale.pack(pady=5)
self.boton_vale.place(x=86, y=50, width=128, height=31)
else:
self.connection2 = connect_to_database()
self.cursor2 = self.connection2.cursor()
# Ejemplo: Ejecutar una consulta para obtener datos
self.cursor2.execute("SELECT nombre,password FROM usuarios")
self.data2 = self.cursor2.fetchall()

for self.fila in self.data2:
self.v_email = self.fila[0]
self.v_password = self.fila[1]

if self.entry_email.get() == self.v_email and self.entry_password.get() == self.v_password:
self.app.root.destroy()
self.ventana_menu = Menus(self)
else:
self.app.root.destroy()
messagebox.showwarning("Advertencia", "Los datos introducidos son incorrectos.")

self.cursor2.close()
self.connection2.close()
Good morning, after spending a while researching and such I have managed to get a result.

is this well done?

Python
def validar(self):

        self.use = self.entry_email.get()
        self.pas = self.entry_password.get()
   
        self.connection2 = connect_to_database()
        self.cursor2 = self.connection2.cursor()
        # Ejemplo: Ejecutar una consulta para obtener datos

        self.consulta = "SELECT * FROM usuarios WHERE nombre = %s AND password = %s"
        #self.cursor2.execute("SELECT nombre,password FROM usuarios")
        self.cursor2.execute(self.consulta, (self.use, self.pas))
        self.data2 = self.cursor2.fetchall()
        #messagebox.showwarning("Advertencia", self.data2)
        # Cerrar el cursor y la conexión

        self.cursor2.close()
        self.connection2.close()

        if self.entry_email.get() == "" or self.entry_password.get() == "":
            self.dialogo = tk.Toplevel()
            self.dialogo.title("ERROR")
            self.dialogo.geometry("300x50")
            self.dialogo.resizable(0,0)

            self.wtotal2 = self.dialogo.winfo_screenwidth()
            self.htotal2 = self.dialogo.winfo_screenheight()
        
            self.wventana2 = 300
            self.hventana2 = 100
        
            self.pwidth2 = round(self.wtotal2/2-self.wventana2/2)
            self.pheight2 = round(self.htotal2/2-self.hventana2/2)
        
            self.dialogo.geometry(str(self.wventana2)+"x"+str(self.hventana2)+"+"+str(self.pwidth2)+"+"+str(self.pheight2))
            self.etiqueta = tk.Label(self.dialogo, text="Rellena todos los campos.", padx=10, pady=10)
            self.etiqueta.pack()

            self.image_boton_vale = Image.open("src/boton_vale.png")
            self.photo_boton_vale = ImageTk.PhotoImage(self.image_boton_vale)

            self.boton_vale = tk.Button(self.dialogo, image=self.photo_boton_vale, command=self.cerrar_dialogo_vale)
            self.boton_vale.pack(pady=5)
            self.boton_vale.place(x=86, y=50, width=128, height=31)

        elif self.data2:
            self.app.root.destroy()
            VariablesGlobales.variable_global1 = self.use
            self.ventana_menu = Menus(self)
        else:
            self.app.root.destroy()
            messagebox.showwarning("Advertencia", "Los datos introducidos son incorrectos.")
 
Share this answer
 
v2
Comments
Andre Oosthuizen 11-Aug-23 9:30am    
@Tomas, please add your code under your original question by selecting "Improve Question". Once under your question, I will give an answer, seems there are some improvements you can make to your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900