简体   繁体   中英

How to get row id on button click?

I'm trying to make an shopping cart by using jsp. I need to get id from a row to add another table using that id. I have table created and buttons placed. What kind of method should I create or what should I do at all? Thanks...

I want to run this sql command to update the cart : " insert into users.cart (Name, Price) select Name, Price from users.product where id = (...);

This is a part of my jsp file that i need to use that button to get id

<tr>

</tr>
<tr>
<td><b>ProductName</b></td>
<td><b>Description</b></td>
<td><b>Price</b></td>
</tr>
<%

try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","");
    Statement st = con.createStatement(); 
    ResultSet rs =st.executeQuery("SELECT * FROM users.product"); 

    out.println("<h1 align = \"center\">Start Shopping:</h1><br>");

    while(rs.next()){
%>
<tr>

<td><%=rs.getString("Name") %></td>
<td><%=rs.getString("Description") %></td>
<td><%=rs.getInt("Price") %></td>
<td><input type = "button" value = "Add to cart"></td>

</tr>

<% 
}

} catch (Exception e) {
e.printStackTrace();
}
%>
</table>

You can use <a> tag to pass id to your jsp page where your query to insert is there like below :

 <td>
 <!-- here you are passing your id=somevalue to yourjsp page-->
   <a href="yourjsp.page?id=<%=rs.getString("id") %>">
    <input type = "button" value = "Add to cart">
   </a>
 </td> 

And to get above id you can do like below :

   String id=request.getParameter("id");//getting id passed in url also you can convert this id to int

Passed id in your query and then redirect to your jsp page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM