This is a beginners attempt for inserting data into a mysql table using flask. I can enter data in the form, but it doesn't insert the data. I'm using pythonanywhere, so the error message is not clear...
Transactions.html:
<h1>Enter a Transaction</h1>
<form action="/transactions" method=post>
<dl>
<dt>Sale Item:
<dd><input type=text name="Item" required/>
<dt>Shack:
<dd><input type=text name="Shack" required/>
<dt>Reference:
<dd><input type=text name="Paym_Reference" required/>
<dt>Amount:
<dd><input type=text name="Amount" required/>
</dl)
<br>
<p><input type=submit value=Enter>
</form>
MySQL: transactions
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| trans_id | int(11) | NO | PRI | NULL | auto_increment |
| Item | varchar(20) | YES | | NULL | |
| Shack | varchar(10) | YES | | NULL | |
| Paym_Reference | varchar(20) | YES | | NULL | |
| Amount | int(10) | YES | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
Code:
import MySQLdb as my
conn = my.connect ('hello.mysql.pythonanywhere-services.com','hello','password','SIIL$Transactions')
c = conn.cursor ()
@app.route('/add_data', methods=['GET', 'POST'])
def add_data():
Item= request.form('Item')
Shack= request.form('Shack')
Paym_Reference= request.form('Paym_Reference')
Amount= request.form('Amount')
c.execute("INSERT INTO transactions(Item, Shack, Paym_Reference, Amount) VALUES ('',%s,%s,%s,%s)")
conn.commit()
return 'Done'
@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
add_data()
return render_template('Transactions.html')