1
  userdetails.jsp

  <tr>

 <td>
 <%
  out.println(rs.getString(1));
  name=rs.getString(1);
  out.print("<input type='hidden' name='user' value='"+name+"'");
  %>

</td>


<td>
<%out.println(rs.getString(2));

    %>
</td>
    <td>
          <%out.println(rs.getString(3));

    %>
    </td>
    <td>
        <%out.println(rs.getString(4));

    %>
    </td>

    <td>
        <input type="Submit" name="delete_user" value="Delete"/>
    </td>

    </tr>

when i click the delete button only first row is getting deleted and not the row corresponding to which button is clicked

Deep
  • 461
  • 5
  • 9
  • 17

2 Answers2

3

You're putting multiple hidden input values and delete buttons altogether in the same form. When you use request.getParameter() to get the hidden input value, you will indeed only get the first one, regardless of the delete button pressed.

You need to group the hidden input and the delete button in their own forms.

<td>
    <form action="delete" method="post">
        <input type="submit" name="delete_user" value="Delete" />
        <input type="hidden" name="user" value="<%=rs.getString(1)%>" />
    </form>
</td>

This way the request will always have the one and the right user name as parameter.


Said that, using scriptlets in JSP is 90's way of writing JSPs and also mingling database logic in a JSP is not really a good practice. I'd suggest to get yourself through this answer.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You can add the user name to the button value upon clicking it:

<input type="Submit" name="delete_user" value="Delete" onclick="this.value += ' <% out.print(name); %>'; this.disabled = true; " />

Then in the server side code, parse the value: get the text after first space, which is the user name to delete and reference only row with that user name.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208