Reputation: 132
I am trying to insert several data in one table to another but it is not taking the data from the HTML, I have tried several ways, I also tried to take the number as a variable and insert it directly but it did not work.
This is what I am trying to do, that 2 tables bring me a value and insert them in a table called quantity, which has 2 ids and only 3 fields, where is the id, the count and the id of the product but when I try Insert the data, the page only reloads.
Python
if request.method == 'POST':
try:
dcont = request.form['dcont']
ids = request.form['ids']
cn = request.form['cn']
with sql.connect("DRIVER={SQL Server}; SERVER=AUS_COMPUTO02\SQLEXPRESS; DATABASE=WEBSERVICE; Trusted_Connection = yes;") as con:
cur = con.cursor()
cur = con.execute("INSERT INTO dbo.DETALLECONTEO VALUES (?,?,?)", (dcont,ids,cn))
cur.commit()
msg = "Successfully added"
print(cur)
print(dcont,ids,cn)
except:
con.rollback()
msg = "error inserte la operacion nuevamente"
finally:
# con.close()
return render_template ('result.html')
return render_template('new.html', prov=prov, cnt=cnt, ind=ind )
HTML
<div class="jumbotron">
<div class="form-group">
<form class="form-inline my-2 my-lg-0" action="{{ url_for('new') }}" class="form-control" class="w-auto p-3">
<select name='prove' class="form-control mr-sm-2" aria-label="Search" >
<option value="0"> Selecione su Proveedor </option>
<br action="{{ url_for('new') }}" method='POST'>
{% for p in prov %}
<option value="{{ p.ID }}" > {{ p.DESCRIPCION }} </option>
{% endfor %}
</select>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" >Seclet</button>
</form>
</div>
<div class="form-group">
<form class="form-inline my-lg-0" action="{{ url_for('adt') }}" method='POST'>
<table class="table" class="form-control">
<thead>
<tr>
<th scope="col">Codigo</th>
<th scope="col">Descipcion</th>
<th acope="col">Cantidad</th>
</tr>
</thead>
<tbody>
{% for c in cnt %}
<tr>
<td >{{ c.codigo }}</td>
<td>{{ c.descripcion }}</td>
<td > <input name='cn' id='cn' type="number" class="form-control"></td>
<td><select name='dcont' id='dcont'>
{% for i in ind %}
<option value=" {{i.Conteo}}"> {{i.Conteo}} </option>
{% endfor %}
</select>
</td>
<td> <select name='ids' id='ids'>
<option value="{{c.id}}"> {{c.id}} </option>
</select>
</td>
{% endfor %}
</tr>
</tbody>
</table>
</tbody>
</table>
<input class="btn btn-outline-success my-2 my-sm-0" type="submit" ><br>
</form>
</div>
</div>
Upvotes: 1
Views: 89
Reputation: 9440
Two main areas to focus on-- first your form hasn't been told to 'POST' the data-- go it's going to send it via the default 'GET' mode, changing your form code, by adding method="post"
will resolve that issue.
<form class="form-inline my-2 my-lg-0" method="post" action="{{ url_for('new') }}" class="form-control" class="w-auto p-3">
So now your form data will get POST'ed-- and so will trigger the if request.method == 'POST':
logic.
Your next issue is your data is coming in as the same named element-- e.g. if you submitted three entries, your ids
element would post:
ids=3
ids=2
ids=6
So we need to use the request.form.getlist('ids')
to get that data, so you end up with a list of entries for the form elements:
all_ids = request.form.getlist('ids')
all_cn = request.form.getlist('cn')
all_dcont = request.form.getlist('dcont')
Now if we looked at that data that might be submitted:
>>> print(all_ids)
['3','2','6']
>>> print(all_dcont)
['A','B','C']
>>> print(all_cn)
['X','Y','Z']
But we need to handle them as rows-- to insert into our database, so we can zip them together to make 'rows' of the data:
rows = zip(all_ids, all_dcont, all_cn)
So now we have:
>>> print(rows)
['3','A','X'], ['2','B','Y'], ['6','C','Z']
Which we can iterate through (theres a number of ways to do this, we'll keep it simple) to create out SQL statements to execute:
for entry in rows:
query_ids = entry[0]
query_dcont = entry[1]
query_cn = entry[2]
...
cur = con.execute("INSERT INTO dbo.DETALLECONTEO VALUES (?,?,?)", (query_dcont,query_ids,query_cn))
...
Upvotes: 1