-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
42 lines (31 loc) · 1.16 KB
/
Copy pathapp.py
File metadata and controls
42 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Flask, render_template, request, redirect, url_for
from flask_mysqldb import MySQL
import os
app = Flask(__name__)
# Configure MySQL
app.config['MYSQL_HOST'] = os.environ.get("MYSQL_HOST", "localhost")
app.config['MYSQL_USER'] = os.environ.get("MYSQL_USER", "user")
app.config['MYSQL_PASSWORD'] = os.environ.get("MYSQL_PASSWORD", "")
app.config['MYSQL_DB'] = os.environ.get("MYSQL_DB", "flask_example")
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/greet', methods=['POST'])
def greet():
name = request.form['name']
age = request.form['age']
cur = mysql.connection.cursor()
# Using parameterized query to prevent SQL injection
query = 'INSERT INTO user (name, age) VALUES (%s, %s)'
values = (name, age)
cur.execute(query, values)
mysql.connection.commit()
# Fetch data to display in the greeting.html template
cur.execute('SELECT * FROM user')
data = cur.fetchall()
cur.close()
return render_template('greeting.html', data=data)
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0")