0

I am making an office management system using Flask and I'm new to it. I'm getting this error which I do not know why is occurring. I have checked the data in my db.yaml file, and it is a-okay. Can someone help me get past this error.

app.py

from flask import Flask, render_template, request
from flask_mysqldb import MySQL
import yaml

app = Flask(__name__)

db = yaml.safe_load(open('db.yaml'))
app.config['MySQL_HOST'] = db['mysql_host']
app.config['MySQL_USER'] = db['mysql_user']
app.config['MySQL_PASSWORD'] = db['mysql_password']
app.config['MySQL_DB'] = db['mysql_db']

mysql = MySQL(app)

@app.route('/')
def output():
    return db['mysql_db']

@app.route('/home')
def index():
    return render_template('index.html')

@app.route('/log_in/<string:name>', methods=['GET', 'POST'])
def manager_log_in(name):
    if request.method == 'POST':
        # Fetching HTML Data
        userDetails = request.form
        username = userDetails['username']
        password = userDetails['password']
        # Fetching Database Data
        cur = mysql.connect.cursor()
        cur.execute(f"SELECT * FROM {name};")
        data = cur.fetchall()

        username_exists = False
        if username in data[:]['username']:
            username_exists = True

        password_matches = False
        cur.execute(f"SELECT password FROM {name} WHERE username = {username};")
        actual_password = cur.fetchall()
        actual_password = actual_password[0]['password']
        if actual_password == password:
            password_matches = True

        if username_exists and password_matches:
            return 'LOGGED IN SUCCESSFULLY'
        else:
            return 'INCORRECT USERNAME OR PASSWORD'

    return render_template('log_in.html', name=name.capitalize())

if __name__ == '__main__':
    app.run(debug=True)

output

MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'hp'@'localhost' (using password: NO)")

log_in.html

{% extends 'base.html' %}

{% block title %}Log_In{% endblock %}

{% block head %}
    <!-- Head Stuff -->
{% endblock %}

{% block body %}
    <h1>{{ name }} Log_In</h1>
    <form action='' method='POST'>
        Username: <input type='text' name='username' id='username_entry'>
        <br>
        Password: <input type='password' name='password' id='password_entry'>
        <br>
        <input type='submit' name='submit_button' id='submit_button'>
    </form>
    <hr>
{% endblock %}

I have attached the code files where the error might be possibly occurring from.

  • Does this answer your question? [Access denied for user 'root@localhost' (using password:NO)](https://stackoverflow.com/questions/2995054/access-denied-for-user-rootlocalhost-using-passwordno) – Shiva Jan 11 '21 at 04:08
  • Actually, It doesn't. I'm sorry, I'm new to programming and I'm having trouble understanding this. – Armaan Randhawa Jan 11 '21 at 04:42
  • Are you able to login into your MySQL DB using username and password? You need to configure your MySQL server first before executing your code. – Shiva Jan 11 '21 at 04:45

0 Answers0