0

我有一个需要学生出勤的页面。它打印出一个学生列表,每个学生旁边都有一个复选框。然后该人单击提交并将所选学生的 ID 和实验室 ID 发送到数据库。

我遇到的问题是将最后一个勾选的框发送到数据库,其余的都没有。我假设它会覆盖它之前的那些,但我不确定如何解决这个问题。

这是我的doPost方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


            int user_id = Integer.parseInt((String)request.getParameter("user_id"));
            int lab_id =  Integer.parseInt((String)request.getParameter("lab_id"));


              System.out.println("I got a blow job");

              String message = null; 
              try {
                  Class.forName("com.mysql.jdbc.Driver");
                  Connection con = 
                    DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
                System.out.println("got connection"); 

                Statement s = con.createStatement(); 

                String sql = "INSERT INTO user_lab" + 
                          " (user_id, lab_id)" + 
                          " VALUES" + 
                          " ('" +  user_id + "'," + 
                             " '"  +  lab_id + "')"; 

                  System.out.println(sql);
                  int i = s.executeUpdate(sql); 
                  if (i==1) {
                    message = "Successful attendance."; 
                    response.sendRedirect("Tutor_labs");
                  } 

                  s.close(); 
                  con.close(); 
                } 
                catch (SQLException e) {
                  message = "Error." + e.toString(); 
                  boolean error = true; 
                } 
                catch (Exception e) {
                  message = "Error." + e.toString(); 
                  boolean error = true; 
                } 
                if (message!=null) {
                  PrintWriter out = response.getWriter(); 
                  out.println("<B>" + message + "</B><BR>"); 
                  out.println("<HR><BR>"); 
                } 

              } 

    }

这是我的 JSP 页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Mars University Lab System</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen">
</head>

<body>
<jsp:include page="headerTutor.jsp"/>

<div id = "centrecontent">
<h3>Students In Tutorial</h3>
<h2>Attendance List</h2>

<%
    String[] list1 = (String[])request.getAttribute("res1");
    String[] list2 = (String[])request.getAttribute("res2");
    String[] list3 = (String[])request.getAttribute("res3");
    String[] list4 = (String[])request.getAttribute("res4");

         if(null == list1){%>

<th>Uni Id</th><th>Name</th><th>Email</th>      
<% 
         }else{ %>

        <form name="ViewStudentsTutor" ACTION="http://www.crackman.net.au/ICE/test.php\"  method="post">
<%      for(int i=0; i<list1.length; i++)
        {   
            %>                  <input type="hidden" name="lab_id" value=<%=request.getParameter("labid")%>>
                                <%out.println(list2[i]);%>
                                <%out.println(list3[i]);%>
                                <%out.println(list4[i]); %>
                                <input type="checkbox" name="user_id" value=<%out.println(list1[i]);%>><br>


    <%  }}
    %>  
     <input type="submit" value="Submit" name="Submit"/>
        </form> 


</div>
<jsp:include page="footer.jsp"/>

</body>
</html>
4

1 回答 1

3

您只使用一个user_idusingrequest.getParameter("user_id") 相反,您应该使用它将返回您想要的所有srequest.getParameterValues("user_id")的数组,而不是您正在获得的单个Stringuser_iduser_id

类似的是lab_id

于 2012-10-07T06:32:19.157 回答