我正在使用eclipse IDE在jsp上工作。我的項目基於員工時間管理系統。因此,我需要將員工每天工作多少小時以及相應的項目名稱。如果我的表格包含一條記錄,它將採集數據並妥善保存。但是我的代碼不適用於多個記錄。它只需要一個記錄。但我的要求是將多個記錄保存在表中。請告訴我如何做到這一點。我想通過jsp將我的表數據保存在mysql數據庫中
**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");
}
%>
的你有什麼期望時,你的'project_name'和'day1'領域均具有相同的名稱? – m02ph3u5
我期待日期字段,即day1將與相關的表標題數據一起保存,即project_name應save.only第一條記錄被保存,但它沒有從第二條記錄中獲取數據 – roja
您必須爲字段創建數組在你的while循環中。例如:'name =「project_name []」' - 看看這個:http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs – m02ph3u5