Select/Option/Value 使用经典 ASP 进行更改
Select/Option/Value on change using Classic ASP
我尝试使用经典 ASP 创建 select 选项值更改,但没有成功。没有错误,但是当我查看时,第二个 select.
没有弹出任何内容
我喜欢select第一个select框中的学生姓名,如果匹配,则第二个框显示课程名称,第三个框显示学期。
我喜欢将学生姓名和 StudentID 存储到数据库中。
有人能帮忙吗?
学生table
StudentID Firstname Lastname
-------------------------------
01 AAA LN1
02 BBB LN2
03 CCC LN3
Class table
Course CourseID StudentID Semester
-------------------------------------
History C01 01 Spring
History C01 01 Summer
Math C02 02 Spring
Math C02 02 Fall
代码
<% Set oRs = Server.CreateObject("adodb.recordset")
strSQL = " SELECT StudentID, Firstname, Lastname FROM Students Where StudentID = '" & Request.ServerVariables("LOGON_Student") & "'"
oRs.Open strSQL, myConn
sName = oRs("Lastname") & ", " & oRs("Firstname")
if not oRs.eof then %>
<select name="Students" id="selectStudent" onChange="this.form.action='default.asp';this.form.submit();">
<option value="<%= oRs(0) %>" <% if trim(request.Form("Students")) = trim(oRs(0)) then response.write " selected "end if %>><%= sName %></option>
<option value="<%= oRs("StudentID") %>" hidden></option>
</select>
<% end if
%>
<% if request.Form("Students") <> "" then
strSQL = " SELECT Course FROM Class WHERE StudentID = '" & request.Form("StudentID") & "'"
Set oRs = Server.CreateObject("adodb.RecordSet")
oRs.Open strSQL, myConn
if not oRs.eof then %>
<select name="Class" id="selectClass" onChange="this.form.action='default.asp';this.form.submit();">
<% do until oRs.eof %>
<option value="<%= oRs(0) %>" <% if trim(request.Form("Class")) = trim(oRs(0)) then response.write " selected "end if %>><%= oRs(0) %></option>
<% oRs.MoveNext
loop %>
</select>
<% end if
end if
%>
<% if request.Form("Class") <> "" then
strSQL = " SELECT Semester FROM Class WHERE Course = '" & request.Form("Course") & "'"
Set oRs = Server.CreateObject("adodb.RecordSet")
oRs.Open strSQL, myConn
if not oRs.eof then %>
<select name="Class" id="selectClass" onChange="this.form.action='default.asp';this.form.submit();">
<% do until oRs.eof %>
<option value="<%= oRs(0) %>" <% if trim(request.Form("Class")) = trim(oRs(0)) then response.write " selected "end if %>><%= oRs(0) %></option>
<% oRs.MoveNext
loop %>
</select>
<% end if
end if
%>
您在 SQL 语句中使用了错误的值。
您应该使用第一个下拉元素的名称,而不是数据库字段的名称。
此外,您最好使用参数,而不是直接注入值,因为这可以让用户控制您的数据库,甚至可能控制整个服务器。 (要了解更多信息,请阅读 SQL 注入攻击。)
所以,正确的 SQL 应该是:
strSQL = "SELECT Course FROM Class WHERE StudentID = ?"
Set oCommand = Server.Createobject("ADODB.Command")
Set oCommand.ActiveConnection = myConn
oCommand.CommandText = strSQL
oCommand.Parameters.Append(oCommand.CreateParameter("StudentID", 3, , , Request.Form("Students")) )
Set oRS = oCommand.Execute
确保从您的代码中删除 oRs.Open strSQL, myConn
行。
如您所见,我将 Request.Form("StudentID")
更改为 Request.Form("Students")
,现在查询应该 return 结果。
我尝试使用经典 ASP 创建 select 选项值更改,但没有成功。没有错误,但是当我查看时,第二个 select.
没有弹出任何内容我喜欢select第一个select框中的学生姓名,如果匹配,则第二个框显示课程名称,第三个框显示学期。
我喜欢将学生姓名和 StudentID 存储到数据库中。
有人能帮忙吗?
学生table
StudentID Firstname Lastname
-------------------------------
01 AAA LN1
02 BBB LN2
03 CCC LN3
Class table
Course CourseID StudentID Semester
-------------------------------------
History C01 01 Spring
History C01 01 Summer
Math C02 02 Spring
Math C02 02 Fall
代码
<% Set oRs = Server.CreateObject("adodb.recordset")
strSQL = " SELECT StudentID, Firstname, Lastname FROM Students Where StudentID = '" & Request.ServerVariables("LOGON_Student") & "'"
oRs.Open strSQL, myConn
sName = oRs("Lastname") & ", " & oRs("Firstname")
if not oRs.eof then %>
<select name="Students" id="selectStudent" onChange="this.form.action='default.asp';this.form.submit();">
<option value="<%= oRs(0) %>" <% if trim(request.Form("Students")) = trim(oRs(0)) then response.write " selected "end if %>><%= sName %></option>
<option value="<%= oRs("StudentID") %>" hidden></option>
</select>
<% end if
%>
<% if request.Form("Students") <> "" then
strSQL = " SELECT Course FROM Class WHERE StudentID = '" & request.Form("StudentID") & "'"
Set oRs = Server.CreateObject("adodb.RecordSet")
oRs.Open strSQL, myConn
if not oRs.eof then %>
<select name="Class" id="selectClass" onChange="this.form.action='default.asp';this.form.submit();">
<% do until oRs.eof %>
<option value="<%= oRs(0) %>" <% if trim(request.Form("Class")) = trim(oRs(0)) then response.write " selected "end if %>><%= oRs(0) %></option>
<% oRs.MoveNext
loop %>
</select>
<% end if
end if
%>
<% if request.Form("Class") <> "" then
strSQL = " SELECT Semester FROM Class WHERE Course = '" & request.Form("Course") & "'"
Set oRs = Server.CreateObject("adodb.RecordSet")
oRs.Open strSQL, myConn
if not oRs.eof then %>
<select name="Class" id="selectClass" onChange="this.form.action='default.asp';this.form.submit();">
<% do until oRs.eof %>
<option value="<%= oRs(0) %>" <% if trim(request.Form("Class")) = trim(oRs(0)) then response.write " selected "end if %>><%= oRs(0) %></option>
<% oRs.MoveNext
loop %>
</select>
<% end if
end if
%>
您在 SQL 语句中使用了错误的值。
您应该使用第一个下拉元素的名称,而不是数据库字段的名称。
此外,您最好使用参数,而不是直接注入值,因为这可以让用户控制您的数据库,甚至可能控制整个服务器。 (要了解更多信息,请阅读 SQL 注入攻击。)
所以,正确的 SQL 应该是:
strSQL = "SELECT Course FROM Class WHERE StudentID = ?"
Set oCommand = Server.Createobject("ADODB.Command")
Set oCommand.ActiveConnection = myConn
oCommand.CommandText = strSQL
oCommand.Parameters.Append(oCommand.CreateParameter("StudentID", 3, , , Request.Form("Students")) )
Set oRS = oCommand.Execute
确保从您的代码中删除 oRs.Open strSQL, myConn
行。
如您所见,我将 Request.Form("StudentID")
更改为 Request.Form("Students")
,现在查询应该 return 结果。