简体   繁体   中英

Issue Login System Python and Mysql

Database Image

Hello . I created a login system with the help of python and MySQL but it does not works correctly because if I input a username which is found in db that username can works with all passwords from 'listforPw ' Please read the following code and see the image that I've uploaded:

    mycursor.execute('SELECT * FROM users')
myresult = mycursor.fetchall()
listforName = []
listforPw = []
checkUsername = input('Insert your username  : ' )
checkPassword = input('Insert your password : ')
for row in myresult:
    listforName.append(row[3])
    listforPw.append(row[4])
if checkUsername in listforName and checkPassword in listforPw:
    print('You have logged in with succes !')
else:
    print('Your credentials do not exist !')

So the question is I do not know how can I check if a username is equal with the password from username's line.

Your code does check for what you described. if checkPassword in listforPw .

Instead of this, query only the user you are interested in and compare the password for the single returned user. Like

mycursor.execute('SELECT * FROM users WHERE username = %s', 'theUserName')

Also, NEVER store passwords this way. Use hashes https://en.wikipedia.org/wiki/Cryptographic_hash_function

Basically, you always hash the user's password when he inputs it, and you also store the passwrod in the hashed format, so when you compare them, they will be equal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM