java - 带有提交按钮的 html 表,用于使用 SQL 数据库中的数据更新每个单独的表行

标签 java html sql-server jsp html-table

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

相关文章:

java - 抽象 ArrayList<E> 迭代以构建可重用的方法

html - Sublime 2,如何自动关闭 HTML 标签并将光标放在标签内

javascript - 从 3 个输入获取并同时替换

php - 替换 HTML 页面中的 iPhone 表情符号

sql-server - decimal 和 bigint 除法时如何定义小数位数?

java - 不使用 BufferedReader 计算一组字符在文件中出现的次数

java - Java Cucumber cli (cucumber.api.cli.Main) 的 --glue 选项的语法

sql-server - 如何停止EF4.1 Code-First为实体PK创建Culstered索引

c# - 使用存储过程从复选框切换 bool 值是否有任何安全优势

java - 将用户定义的 java 代码添加到库