通过循环插入记录

时间:2013-07-26 07:42:33

标签: ms-access asp-classic

我有一个表格,其中值来自数据库,所有输入字段都有自己的名称,并且它们是循环的。当我提交表单时,数据插入与每行的每个单元格中的循环相等:(

以下是截图正确解释

enter image description here

以下是我从中获取记录的表格。

<%
While Not RStmtbl.EOF
%>
<tr>
<td><input type="text" value="<% Response.Write(Rstmtbl("tdate"))%>" name="tdate_insert" readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("day"))%>" name="day_insert" readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("class"))%>" name="class_insert" readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("pno"))%>" name="pno_insert" readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("subject"))%>" name="subject_insert" readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("teacher"))%>" name="teacher_insert" readonly /></td>
<td>
<select name="status_insert">
<option selected>Choose Status</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<input type="hidden" value="<% Response.Write(formno) %>" name="uid_insert">
<input type="hidden" value="<% Response.Write(Now) %>" name="sdate_insert">
</td>
</tr>
<%
RStmtbl.Movenext
Wend
%>

以下是插入代码

<%
if request.Form("TcfRecord")="Insert" then
Set InsCom=Server.CreateObject("ADODB.Command")
InsCom.ActiveConnection=objConn

InsTD = Trim(request.Form("tdate_insert"))
InsTD = replace(InsTD,"'","''")

InsTD2 = Trim(request.Form("day_insert"))
InsTD2 = replace(InsTD2,"'","''")

InsCL = Trim(request.Form("class_insert"))
InsCL = replace(InsCL,"'","''")

InsPI = Trim(request.Form("pno_insert"))
InsPI = replace(InsPI,"'","''")

InsSBI = Trim(request.Form("subject_insert"))
InsSBI = replace(InsSBI,"'","''")

InsTI = Trim(request.Form("teacher_insert"))
InsTI = replace(InsTI,"'","''")

InsSI = Trim(request.Form("status_insert"))
InsSI = replace(InsSI,"'","''")

InsUI = Trim(request.Form("uid_insert"))
InsUI = replace(InsUI,"'","''")

InsDI = Trim(request.Form("sdate_insert"))
InsDI = replace(InsDI,"'","''")

For Each item in Request.Form
InsCom.CommandText = "Insert into time_table_status(tdate, [day], [class], pno, subject, teacher, status, uid, submit_date)Values(?,?,?,?,?,?,?,?,?)"
InsCom.Parameters.Append InsCom.CreateParameter("@tdate_insert", adVarChar, adParamInput, 255, InsTD)
InsCom.Parameters.Append InsCom.CreateParameter("@day_insert", adVarChar, adParamInput, 255, InsTD2)
InsCom.Parameters.Append InsCom.CreateParameter("@class_insert", adVarChar, adParamInput, 255, InsCL)
InsCom.Parameters.Append InsCom.CreateParameter("@pno_insert", adVarChar, adParamInput, 255, InsPI)
InsCom.Parameters.Append InsCom.CreateParameter("@subject_insert", adVarChar, adParamInput, 255, InsSBI)
InsCom.Parameters.Append InsCom.CreateParameter("@teacher_insert", adVarChar, adParamInput, 255, InsTI)
InsCom.Parameters.Append InsCom.CreateParameter("@status_insert", adVarChar, adParamInput, 255, InsSI)
InsCom.Parameters.Append InsCom.CreateParameter("@uid_insert", adVarChar, adParamInput, 255, InsUI)
InsCom.Parameters.Append InsCom.CreateParameter("@sdate_insert", adVarChar, adParamInput, 255, InsDI)

InsCom.Execute

Next

end if
%>

每行的每个单元格中应该有一个值。请帮我识别错误......

1 个答案:

答案 0 :(得分:2)

您的表单构建代码应该是这样的:

<%
Dim counter : counter = 1
While Not RStmtbl.EOF
%>
<tr>
<td><input type="text" value="<% Response.Write(Rstmtbl("tdate"))%>" name="tdate_insert_<%=counter%>" readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("day"))%>" name="day_insert_<%=counter%>"  readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("class"))%>" name="class_insert_<%=counter%>"  readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("pno"))%>" name="pno_insert_<%=counter%>"  readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("subject"))%>" name="subject_insert_<%=counter%>"  readonly /></td>
<td><input type="text" value="<% Response.Write(Rstmtbl("teacher"))%>" name="teacher_insert_<%=counter%>"  readonly /></td>
<td>
<select name="status_insert_<%=counter%>">
<option selected>Choose Status</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<input type="hidden" value="<% Response.Write(formno) %>" name="uid_insert_<%=counter%>">
<input type="hidden" value="<% Response.Write(Now) %>" name="sdate_insert_<%=counter%>">
</td>
</tr>
<%
RStmtbl.Movenext
counter =counter+1
Wend
%>
<input type="hidden" value="<% =counter%>" name="rowcount">

然后在您的保存页面中:

<%
if request.Form("TcfRecord")="Insert" then
Dim maxcount : maxcount = Cint(Request("rowcount"))
Dim counter 

for counter =1 to maxcount-1 

Set InsCom=Server.CreateObject("ADODB.Command")
InsCom.ActiveConnection=objConn

InsTD = Trim(request.Form("tdate_insert_" & CStr(counter ) ))
InsTD = replace(InsTD,"'","''")

InsTD2 = Trim(request.Form("day_insert_" & CStr(counter ) ))
InsTD2 = replace(InsTD2,"'","''")

InsCL = Trim(request.Form("class_insert_" & CStr(counter ) ))
InsCL = replace(InsCL,"'","''")

InsPI = Trim(request.Form("pno_insert_"&  CStr(counter ) ))
InsPI = replace(InsPI,"'","''")

InsSBI = Trim(request.Form("subject_insert_"&  CStr(counter ) ))
InsSBI = replace(InsSBI,"'","''")

InsTI = Trim(request.Form("teacher_insert_" & CStr(counter ) ))
InsTI = replace(InsTI,"'","''")

InsSI = Trim(request.Form("status_insert_" & CStr(counter ) ))
InsSI = replace(InsSI,"'","''")

InsUI = Trim(request.Form("uid_insert_"& CStr(counter ) ))
InsUI = replace(InsUI,"'","''")

InsDI = Trim(request.Form("sdate_insert_" & CStr(counter ) ))
InsDI = replace(InsDI,"'","''")


InsCom.CommandText = "Insert into time_table_status(tdate, [day], [class], pno, subject, teacher, status, uid, submit_date)Values(?,?,?,?,?,?,?,?,?)"
InsCom.Parameters.Append InsCom.CreateParameter("@tdate_insert", adVarChar, adParamInput, 255, InsTD)
InsCom.Parameters.Append InsCom.CreateParameter("@day_insert", adVarChar, adParamInput, 255, InsTD2)
InsCom.Parameters.Append InsCom.CreateParameter("@class_insert", adVarChar, adParamInput, 255, InsCL)
InsCom.Parameters.Append InsCom.CreateParameter("@pno_insert", adVarChar, adParamInput, 255, InsPI)
InsCom.Parameters.Append InsCom.CreateParameter("@subject_insert", adVarChar, adParamInput, 255, InsSBI)
InsCom.Parameters.Append InsCom.CreateParameter("@teacher_insert", adVarChar, adParamInput, 255, InsTI)
InsCom.Parameters.Append InsCom.CreateParameter("@status_insert", adVarChar, adParamInput, 255, InsSI)
InsCom.Parameters.Append InsCom.CreateParameter("@uid_insert", adVarChar, adParamInput, 255, InsUI)
InsCom.Parameters.Append InsCom.CreateParameter("@sdate_insert", adVarChar, adParamInput, 255, InsDI)

InsCom.Execute

Next

end if
%>

我没有启用asp的机器来运行它并立即检查错误,但这是一般的想法。

如果您有问题,请发布。