更新SQL Server:在While循环中使用html输入 - asp经典

问题描述 投票:0回答:1

对这种语言表示抱歉,我正在维护一个属于ASP Classic的遗留系统。

我有一个html页面,其中包含通过ASP从我的SQL Server生成的内容

<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>

然后,我使用我的dB中的while循环生成内容,正确显示内容:

<%
 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>

现在,在我的桌子上方,我有一个按钮

<button type="submit" value="Submit" class="btn btn-primary mb-2" name="B1">Confirm</button>

当我的最终用户点击提交时,我希望所有的值都更新到我的SQL服务器,现在因为这是在for循环中我不知道更新查询将去哪里..我到目前为止

<%
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)

(请注意我的代码在我的IDE中正确缩进)

现在,当我在While循环中单击提交时,我得到用逗号分隔的ID号,所以我知道它正在更新,但我真的不确定我做错了什么..?

任何帮助将非常感激。

如果需要任何进一步的信息,请告诉我。

预期输出是在网站上显示一些商品代码代码并从用户那里获得响应,然后将该输出写入我的SQL dB,其中Article = Article和ID = ID。

对生成内容的主要查询(这与我的示例数据不匹配,但我会发布错误是在查询本身)

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)
asp-classic
1个回答
0
投票

希望我能正确理解您,您已在数据库中拥有id,quant和article,并希望更新与ID相关的quant。在这种情况下:

在创建表单的循环中,为每个数字输入一个名称中包含ID的名称(或者如果您只有一个字段,则只需将ID作为名称)。在你的情况下:

<input type="number" name="qty<%=rs("ID")%>">

在操作页面上,然后循环遍历所有字段并相应地更新,要么替换名称以获取ID,要么只使用ID作为字段名称,它可以无需替换:

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

这样你就可以浏览表格中的所有字段。我也不确定你在where子句中是否需要ID和Article?一个ID可以有多个文章,反之亦然?

© www.soinside.com 2019 - 2024. All rights reserved.