我尝试使用经典ASP在更改时创建一个选择选项值,但它不起作用。没有错误,但是当我查看时,没有任何内容从第二次选择中弹出。
我喜欢从第一个选择框中选择学生姓名,如果匹配,则第二个框显示课程名称,第三个框显示学期。
我喜欢将学生姓名和StudentID存储到数据库中。
有人可以帮忙吗?
学生表
StudentID Firstname Lastname
-------------------------------
01 AAA LN1
02 BBB LN2
03 CCC LN3
班级表
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
%>
答案 0 :(得分:0)
您在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")
,现在查询应返回结果。