我有一个 html 表,它使用 for 循环从 SQL 数据库打印出信息。 html 表格的每一行都有一个提交按钮。当用户单击此按钮时,该行会更新。此命令有效,但如果数据库中有多行具有相同主键,则仅最后一行更新,并且数据库中具有相同主键的所有行的值将设置为与表中最后一行相同的值。
我需要表中每一行上的按钮仅更新单行,而不是具有相同标识符的多行。我对此很陌生,因此我们将不胜感激。
以下是我一直使用的代码:
<html>
<%
Connection objConn = null;
MedicationList objMedicationList=null;
Medication [] objMedication;
OutputLogs objLog=null;
int intDebug=0;
String strDebug;
String sessionUsername, sessionHCN;
String pmedicationtype=null;
String pdose=null;
String pinstructions=null;
String pcomments=null;
int pvalid=0;
//try{
objConn=(Connection)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_DBCONNECTION);
objLog=(OutputLogs)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_DEBUG_OBJECT);
intDebug=(int)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_DEBUG_FLAG);
//objPortalUserList=new PatientDetailsList(intDebug,objLog);
objLog=new OutputLogs(intDebug);
objMedicationList=new MedicationList(intDebug,objLog);
sessionUsername=(String)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_USERNAME);
sessionHCN=(String)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_HCN);
if (intDebug==1){
objLog.SendToDebug("Getting medication");
}
objMedication=objMedicationList.GetUserMedication(objConn, sessionHCN);
<table style="width:100%">
<tr>
<th>Medication</th>
<th>Dose</th>
<th>Instructions</th>
<th>Comments</th>
</tr>
<% for(int count = 0; count < objMedication.length; count++){%>
<form method="post" action="MedicationPage.jsp">
<tr>
<td><%out.println(objMedication[count].GetPatientMedication());%></td>
<td><%out.println(objMedication[count].GetDose());%></td>
<td><%out.println(objMedication[count].GetHowOften());%></td>
<td><%out.println(objMedication[count].GetComments());%></td>
<td><input type="button" value="Request"</td>
<%
pvalid=Integer.valueOf(objMedication[count].GetValid());
pmedicationtype=String.valueOf(objMedication[count].GetPatientMedication());
if(pvalid!=0){
pvalid=pvalid-1;
objMedicationList.editPrescription(objConn, sessionHCN, pmedicationtype, pvalid);
}
}
%>
</tr>
</form>
</table>
最佳答案
...multiple rows with the same primary key in the database only the final row updates and the values for all rows in the database with the same primary key are set to the same value as the final row in the table...
如果您的数据库表中确实没有唯一的主键,那么这将始终失败。看这里:http://en.wikipedia.org/wiki/Unique_key
关于java - 带有提交按钮的 html 表,用于使用 SQL 数据库中的数据更新每个单独的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529118/