Forums

Displaying index.html but no data?

Link to the image

Also I am using mysql connector. Is this an issue?

db = mysql.connector.connect(
    #credentials
    host="itookurjob.mysql.pythonanywhere-services.com",
    user="itookurjob",
    password="__", #set in database
    database="hwcc_tally" #i tried to avoid common mistake of prefixing with $
)

Not sure what else I can do.

My folder structure:

hwcc
├── __pycache__
│   └── run.cpython-310.pyc
├── run.py
└── static
    ├── index.html
    └── styles.css

In flask I use this:

@app.route("/")
def display_table():
    cursor = db.cursor()
    cursor.execute("SELECT * FROM hwcc_entrants")
    results = cursor.fetchall()  # Fetch all rows
    col_names = [desc[0] for desc in cursor.description]

    return render_template("index.html", data=results,col_names=col_names)  # Pass data to template

@app.route("/process_data",methods=["POST"])
def send_data():

    entries = int(request.form["input_entries"]) #linked to html name attribute

    #did we actually forget the insert statement syntax :lol
    sql = "INSERT INTO hwcc_entrants (total_entrants) VALUES (%s)" #note entrant_count is taken from above var
    # Execute SQL statement
    with db.cursor() as cursor:
        cursor = db.cursor()
        cursor.execute(sql, (entries,)) #adding comma to introduce a tuple?
        db.commit()

    # # Close connections - with takes care of cursor.close
    # cursor.close()
    ## intentionally removed this to keep db connections open otherwise unexpected results?
    # db.close()


    return redirect("/") #redirection after submission

Here is my html snippet:

<!DOCTYPE html>
<html>
<head>
    <title>Enter Data</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>

<body>
    <p>Inserts record to database for tallying number of entrants every week</p>

    <!-- Form for inserting records -->
    <form action="process_data" method="POST">
        <button type="submit">Insert record</button>
        <!-- The name attribute is passed onto Jinja within send_data function -->
        <input name="input_entries" type="number">
    </form>
        <!-- This is my database table -->
        <table>
            <thead>
                <tr>
                    {% for column in col_names %}
                        <th>{{ column }}</th>
                    {% endfor %}
                </tr>
            </thead>
            <tbody>
                {% for row in data %}
                    <tr>
                        {% for item in row %}
                            <td>{{ item }}</td>
                        {% endfor %}
                    </tr>
                {% endfor %}
            </tbody>
        </table>

</body>
</html>

Database name is itookurjob$hwcc_tally indeed. With $