问题
Apologies about the language, I'm maintaining a legacy system which is all in ASP Classic.
I have a html page which has content generated from my SQL Server via ASP
<form method="POST">
<div class="container">
<table id="table" class="table">
<thead class="thead-dark">
<tr>
<th scope="col" data-field="article">Article</th>
<th scope="col" data-field="item">QTY/th>
I then generated the content with a while loop from my dB which displays the content correctly :
<%
While not grs.eof
%>
<tr>
<!--SQL query from ASP-classic-->
<th><%=grs.fields("Article")%></th>
<input type="number" class="form-control" id="Quant" placeholder="<%=grs.fields("QTY")%>" name="qty"></th>
<%
grs.movenext
wend
%>
</tr>
</table>
</form>
Now, above my table I have a button
<button type="submit" value="Submit" class="btn btn-primary mb-2" name="B1">Confirm</button>
When my end user clicks submit, I want all the values to be updated into my SQL server, now as this is within a for loop I wasn't sure where the Update query would go.. I have this so far
<%
If request.form("B1")="Submit" Then
If QTY = "" Then
QTY = 0
Else
QTY = request.form("QTY")
'Update SQL'
gsSQL2 = "UPDATE dB SET quant ='" & QTY & "' WHERE ID = '" & request.querystring("ID") & "' And Article = '" & grs.fields("Article") &"'"
gobjConn.Execute(gsSQL2)
(please note my code is indented properly in my IDE)
Now, when I click submit within the While loop I get the ID number seperated by a comma, so I know it's updating but I'm really unsure as to what I'm doing wrong..?
any help would be much appreciated.
if any further information is needed let me know.
Expected output is to display some Article Codes codes on a website and take a response from the user, then write that output to my SQL dB where the Article = Article and ID = ID.
main query to generated content (this doesn't match my sample data but I'll post incase the mistake is in the query itself)
gsSQL = "SELECT ID, Article, [Item Name] as name, [Stock Quantity] as qty, Built, Boxed, Actual from dB where Store ='" & request.querystring("store") & "' order by [Category], name "
Set grs = gobjConn.Execute(gsSQL)
回答1:
With hopes that I understand you correctly, you have id, quant, and article already in database and want to update the quant related to the ID. In that case:
In the loop that creates the form give each number input a name with the ID in the name (or if you just have one field just have the ID as the name). In your case:
<input type="number" name="qty<%=rs("ID")%>">
On the action page you then loop through all the fields and update accordingly, either you do a replace on the name to get the ID, or if you only use the ID as fieldname it works without replace:
For Each Item In Request.Form
fieldName = Item 'This is the ID (with above field name: qtyID where ID is the ID from the DB.
fieldValue = Request.Form(Item) 'This is the Quantity of that Article ID
articleid=replace(fieldName,"qty","")
sql="update table set qty=" + fieldValue + " where id=" + articleid
Next
That way you will go through all the fields in the form. I am also unsure if you need both ID and Article in where clause? Can one ID have many Articles or vice versa?
来源:https://stackoverflow.com/questions/55831029/update-sql-server-with-html-input-within-a-while-loop-asp-classic