I have an HTML form that looks like this:
<form>
Event Name:<br>
<input type="text" name="eventname">
<br>
Sports:<br>
<select name = "dropdown">
<option value = "soccer">Soccer</option>
<option value = "basketball">Basketball</option>
<option value = "baseball">Baseball</option>
<option value = "football">Football</option>
</select>
<br>
Location:<br>
<input type="text" name="location">
<br>
Time:<br>
<input type="text" name="time">
<br>
Number of Players:<br>
<input type="text" name="numplayers">
<br>
Minimum Age:<br>
<input type="text" name="minage">
<br>
Maximum Age:<br>
<input type="text" name="maxage">
<br>
Minimum Skill Level:<br>
<input type = "radio" name = "minskill" value = "1"> 1
<input type = "radio" name = "minskill" value = "2"> 2
<input type = "radio" name = "minskill" value = "3"> 3
<input type = "radio" name = "minskill" value = "4"> 4
<input type = "radio" name = "minskill" value = "5"> 5
<br>
Maximum Skill Level:<br>
<input type = "radio" name = "maxskill" value = "1"> 1
<input type = "radio" name = "maxskill" value = "2"> 2
<input type = "radio" name = "maxskill" value = "3"> 3
<input type = "radio" name = "maxskill" value = "4"> 4
<input type = "radio" name = "maxskill" value = "5"> 5
<br>
<br>
<input type="submit" value="Submit">
</form>
Looks Like:
This pretty much gets the information needed to create an event.
My goal is to use the following query to post a data into my MySQL database:
INSERT INTO Events(EventName, Sports, Location, Time, NumPlayers, MinAge, MaxAge, MinSkill, MaxSkill)
where the values are the values that the users type into the input form.
In my routes.py
file, where I use flask and MySQLdb, I try to execute a SQL query as follows:
from flask import *
from functools import wraps
import MySQLdb
conn = MySQLdb.connect(...)
@app.route('/hello')
@login_required
def hello():
cursor = conn.cursor()
cursor.execute("SELECT * FROM sports")
rows = cursor.fetchall()
data = [row for row in rows]
cursor.close()
return render_template('hello.html', data = data)
HTML that displays the info above:
<h3>Sports Data</h3>
{% for item in data %}
{{ item }}<br/>
{% endfor %}
This successfully displays the data in the sports
table.
But I have no idea how I should use the input form data to insert the event data into my MySQL database.
Please help!