我正在使用eclipse IDE处理 jsp 。我的项目基于员工时间管理系统。因此,我需要保存员工每天工作的小时数以及相应的项目名称。如果我的表包含一条记录,它会获取数据并正确保存。但是我的代码不能处理多个记录。它只需要一个记录。但我的要求是在表格中保存多个记录。请告诉我如何做到这一点。
**my form(user.php)**
<form method="post" action="addhours.jsp">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Project</th>
<th><input type="text" name="date" value="<%=d.format(cal.getTime())%>" readonly></th>
<th><input type="text" name="date" value="<%=d.format(cal2.getTime())%>" readonly></th>
<th><input type="text" name="date" value="<%=d.format(cal3.getTime())%>" readonly></th>
<th><input type="text" name="date" value="<%=d.format(cal4.getTime())%>" readonly></th>
<th><input type="text" name="date" value="<%=d.format(cal5.getTime())%>" readonly></th>
</tr>
</thead>
<tbody>
<% while (rs.next()) { %>
<tr>
<th><input type="text" name="project_name" value="<% out.println(rs.getString("project_name")); %>" readonly></th>
<td>
<input type="text" name="day1" class="form-control">
</td>
</tr>
<% } %>
</tbody>
</table>
<div class="panel-heading no-collapse text-center" style="font-size:14px">
<input type="submit" name="submit" value="Save">
</div>
</form>
**code to save the data(addhours.php)**
<%
String project_id = null;
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st = con.createStatement();
String hours = request.getParameter("day1");
String userid = (String)session.getAttribute("userid");
String project_name = request.getParameter("project_name");
out.println(project_name);
String date = request.getParameter("date");
session.setAttribute("hours", hours);
out.println(hours);
ResultSet rs;
rs = st.executeQuery("select * from project_task where project_name = '"+project_name+"'");
if (rs.next())
project_id=rs.getString("project_id");
if (hours != null && hours != "") {
int i=st.executeUpdate("insert into workhours(userid,hours,project_id,date) values ('"+userid+"','"+hours+"','"+project_id+"','"+date+"')");
out.println("saved");
}
%>
答案 0 :(得分:0)
使用while loop
代替if
声明
rs=st.executeQuery("select * from project_task where project_name='"+project_name+"'");
while(rs.next())
project_id=rs.getString("project_id");
if(hours!=null && hours!=""){
int i=st.executeUpdate("insert into workhours(userid,hours,project_id,date) values ('"+userid+"','"+hours+"','"+project_id+"','"+date+"')");
out.println("saved");
}