我有一个名为tbl_Teacher
的表,其数据为:
Teacher_ID Lastname Firstname Department
1 aaa Joe Math
2 bbb Jenny History
3 ccc Mark Math
我创建了一个ASP SELECT
。如果我选择" aaa,Mark"从下拉列表中,然后"匹配"将在第二个下拉列表中自动选择。但是,选择Last和Firstname后,它没有任何值;它显示没有找到任何部门。
我做错了什么?有人可以帮忙吗?
非常感谢。
以下是代码:
<% Set rsSelect = Server.CreateObject("adodb.recordset")
strSQL = "SELECT Lastname, Firstname, Teacher_ID FROM tbl_Teacher;"
rsSelect.Open strSQL, strCon
Dim strID
strID = Request.QueryString("Teacher_ID")
if not rsSelect.eof then %>
<select name="Name" onChange="this.form.action='Teacher.asp';this.form.submit();">
<option value="1">Select a Teacher</option>
<% do until rsSelect.eof %>
<option value="<%= rsSelect(0) %>" <% if trim(request.Form("Name")) = trim(rsSelect(0)) then response.write " selected "end if %>><%= rsSelect("Lastname") %>, <%= rsSelect("Firstname") %></option>
<% rsSelect.MoveNext
loop %>
</select>
<% end if
if trim(request.Form("Name")) <> "" then
strSQL = "SELECT Department FROM tbl_Teacher WHERE Teacher_ID = '" & Request.Form("strID") & "';"
Set rsSelect = Server.CreateObject("adodb.RecordSet")
rsSelect.Open strSQL, strCon
if not rsSelect.eof then %>
<select name="Department">
<% do until rsSelect.eof %>
<option value="<%= rsSelect("Department") %>"><%= rsSelect("Department") %></option>
<% rsSelect.MoveNext
loop %>
</select>
<% else %>
<i>No Department found</i>
<% end if
else
response.write "<i>Select a Department</i>"
end if %>
答案 0 :(得分:0)
使用teacher_id
比使用名称或代码要好得多:
<%Set rsSelect = Server.CreateObject("adodb.recordset")
strSQL = "SELECT Teacher_ID, Lastname, Firstname FROM tbl_Teacher;"
rsSelect.Open strSQL, strCon, 1, 1
Dim strID
strID = Request.QueryString("Teacher_ID")
if not rsSelect.eof then %>
<select name="Name" onChange="this.form.action='Teacher.asp';this.form.submit();">
<option value="1">Select a Teacher</option>
<% do until rsSelect.eof %>
<option value="<%= rsSelect(0) %>" <% if request.Form("teacherId") = rsSelect(0) then response.write " selected "end if %>><%= rsSelect("Lastname") %>, <%= rsSelect("Firstname") %></option>
<% rsSelect.MoveNext
loop %>
</select>
<% end if
但是,根据您从数据库中恢复的内容,您在下面的代码中遇到了另一个问题。我建议您重新考虑您的设计,因为您在这里会让自己感到困惑,并在以后将错误的代码打开。尝试将Departments
分成自己的表中的简洁值列表;这将防止任何人在以后进入不受欢迎或拼写错误的部门。然后,您需要以下内容来替换您拥有的内容......
if request.Form("teacherId")) <> "" then
Set rsSelect = Server.CreateObject("adodb.RecordSet")
strSQL = _
"SELECT " & _
"tbl_department.departmentId, " & _
"tbl_department.departmentName, " & _
"tbl_teacher.department " & _
"FROM " & _
"tbl_departments " & _
"LEFT JOIN " & _
"tbl_teacher " & _
"ON tbl_departments.departmentId = tbl_teacher.department AND " & _
"tbl_teacher.teacherId = " & request.Form("teacherId")
rsSelect.Open strSQL, strCon
if not rsSelect.eof then %>
<select name="Department">
<% do until rsSelect.eof %>
<option <%if rsSelect("department")&""<>"" then response.write(" selected ")%> value="<%= rsSelect("Department") %>"><%= rsSelect("Department") %></option>
<% rsSelect.MoveNext
loop %>
</select>
<% else %>
<em>No Department found</em>
<% end if
else %>
<em>Select a Department</em>
<% end if %>
但是...... 这里的SQL安全性存在很大问题 - 上面的代码让您对SQL injection attacks敞开大门,应该仔细考虑。