Hi When i’m trying to update a jdbc table only first row gets updated below is the code for it. though i have an individual button for every row any button i clicks takes input values as first row’s.
<tr>
<td><%=rs.getString("DBID")%></td>
<td><input type="text" name="prev" id="prev" value="<%=rs.getString("Query_Raised")%>" border=''></td>
<td><%=rs.getString("TR")%> </td>
<td><%=rs.getString("Query_Answered")%></td>
<td><%=rs.getString("TA")%></td>
<td><input type="submit" value="Edit">
</tr>
and for comparing i used the below(for where condition) and it is also taking only the first row value
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body><form method="post" action="Up_Query_DB.jsp">
<table><tr><td> <input type="text" id="xyz" name="xyz" value="<%=request.getParameter("prev")%>"></td></tr>
<tr><td><INPUT TYPE="TEXT" NAME="updat" id="updat"></td></tr>
<tr><td><input type="submit" value="Update"></td></tr></table></form>
</body>
and the update i used is
<% try
{
String sc=request.getParameter("xyz");
String upd=request.getParameter("updat");
ps=con.prepareStatement("Update Scope1 Set Query_Raised='"+upd+"' where Query_Raised='"+sc+"'");
int i=ps.executeUpdate();
if(i==1)
{
String redirectURL= "View Queries.jsp";
response.sendRedirect(redirectURL);
}
else{
out.print("Erro");
}
}
catch(Exception e)
{
out.println("error");
}%>
Thanks
1) It helps to write valid HTML
<td><input type="submit" value="Edit"></td>2) Even though you have an Edit button for every row, you have no way of knowing which of the buttons was actually pressed when submitting the form – they are all the same.
Most common way of implementing multi-row edit is to have a separate form for each row. Unless you want to be able to edit multiple rows and then submit all changes at once in which case you will need to distinguish between rows’ data – by giving your data input fields unique names.
Later edit:
3) updating table rows based on value that you intend to change is bad idea for various reasons, one of them is that your query may update the rows that you never knew even existed.
Above code is common for single row update option. Then your update SQL will look something like: