【问题标题】:html table with submit button to update each individual table row using data from SQL database带有提交按钮的 html 表使用 SQL 数据库中的数据更新每个单独的表行
【发布时间】:2014-10-23 13:23:49
【问题描述】:

我有一个 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> 

【问题讨论】:

  • 我遗漏了一些代码我只包含了与问题相关的代码
  • 不,你的html不完整,例如元素按钮没有关闭。
  • 根据定义,“主键”是用于唯一标识行的键。如果您有两行具有相同的主键,那么您的数据设计不佳。可以有一个根本没有主键的表,但如果你没有唯一的字段组合,你就不能指望能够区分两行。
  • 我在数据库表中没有主键,但使用'not null'来确保填充必要的列并在sql命令中使用比较运算符来确保打印正确的数据html表

标签: java html sql-server jsp html-table


【解决方案1】:

...数据库中具有相同主键的多行只有最后一行更新和值 数据库中具有相同主键的所有行都设置为与最后一行相同的值 表...

如果您的数据库表中确实没有唯一的主键,这将一直失败。看这里:http://en.wikipedia.org/wiki/Unique_key

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多