I have a HTML site that has a table that pulls up information for a user to edit. The issue am running into is when I try and insert the rows of this HTML table back to a SQL table. I got as far as inserting records to the SQL table but it is inserting 5 rows instead of the 4 it should in my test data. It also will take the first row in the HTML table and inserts the same values 5 times instead of each row's different values. Am pretty sure am doing the for loops wrong but have been stuck for awhile trying to figure this out. Any help would be appreciated.
HTML:
<body>
<h1>Edit Form</h1>
<form action="#" method="POST">
<form action="/submit" method="post">
<table>
<thead>
<tr>
<th>BOL No</th>
<th>Qty</th>
<th>Heat</th>
<th>ITEM DESC</th>
<th>WEIGHT</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td><input type="text" name="BOLno" value="{{ BOLno }}"></td>
<td><input type="number" name="SERLTQTY" value="{{ row.SERLTQTY }}"></td>
<td><input type="text" name="SERLTNUM" value="{{ row.SERLTNUM }}"></td>
<td><input type="text" name="ITEMDESC" value="{{ row.ITEMDESC }}"></td>
<td><input type="number" name="WEIGHT" value="{{ row.WEIGHT }}"></td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit">Submit</button>
</form>
</body>
Python:
@views.route('/edit/<bol>', methods=['POST', 'GET'])
def editbol(bol):
sql_conn = odbc.connect('DRIVER=xxxxxxxxxxxxx;SERVER=xxxxxx;DATABASE=xxxxxxx;UID=xxxxxxx;PWD=xxxxxxx;')
csr = sql_conn.cursor()
BOLno = None
SERLTQTY = None
ITEMDESC = None
WEIGHT = None
if request.method == 'GET':
csr.execute(f"select * from BOL_VIEW where bolno = {bol}")
data = csr.fetchall()
return render_template('edit.html', data = data, BOLno = bol)
elif request.method == 'POST':
formdata = []
for x in request.form:
BOLno = request.form['BOLno']
SERLTQTY = request.form['SERLTQTY']
SERLTNUM = request.form['SERLTNUM']
ITEMDESC = request.form['ITEMDESC']
WEIGHT = request.form['WEIGHT']
formdata.append((BOLno, SERLTQTY, SERLTNUM, ITEMDESC, WEIGHT ))
for line in formdata:
csr.execute("INSERT INTO BOL_HIST_Rev2 (BOLno, SERLTQTY, SERLTNUM, ITEMDESC, WEIGHT) VALUES (?, ?, ?, ?, ?)", (BOLno, SERLTQTY, SERLTNUM, ITEMDESC, WEIGHT))
sql_conn.commit()
csr.close()
sql_conn.close()
return f'<h1>{bol} insert</h1>'
else:
return f'<h1>{bol} end</h1>'