从jsp页面编辑数据库中的表
我创建了一个从jsp页面编辑数据库中的表,jsp,Jsp,我创建了一个editbooks.jsp页面。问题是它将我重定向到admin.jsp页面,而不更新表 更清楚的是,editbooks.jsp不起作用 我的代码看起来不错,但我无法重新定位问题所在 //editbooks.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import='j
editbooks.jsp
页面。问题是它将我重定向到admin.jsp
页面,而不更新表
更清楚的是,editbooks.jsp
不起作用
我的代码看起来不错,但我无法重新定位问题所在
//editbooks.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import='java.io.*,java.sql.*' %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="editbooks.jsp">
<%
if(request.getParameter("add")!=null)
{
try
{
String id=request.getParameter("id");
String bname=request.getParameter("bname");
String author=request.getParameter("author");
String pub=request.getParameter("pub");
String price=request.getParameter("price");
String cat=request.getParameter("cat");
String qty=request.getParameter("qty");
String pic=request.getParameter("pic");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/bookstore?user=root&password=admin");
String update="update books set bname=?,author=?,pub=?,price=?,cat=?,qty=?,pic=? where bid=?";
PreparedStatement ps=con.prepareStatement(update);
ps.setString(1, bname);
ps.setString(2, author);
ps.setString(3, pub);
ps.setString(4, price);
ps.setString(5, cat);
ps.setString(6, qty);
ps.setString(7, pic);
ps.setString(8, id);
ps.executeUpdate();
response.sendRedirect("admin.jsp");
}
catch(Exception e)
{
out.println(e.getMessage());
}
}
%>
<table>
<tr>
<td><label>Book Name:</label></td>
<td><input type="text" name="bname" id="bname"/></td>
</tr>
<tr>
<td><label>Author:</label></td>
<td><input type="text" name="author" id="author"/></td>
</tr>
<tr>
<td><label>Publication:</label></td>
<td><input type="text" name="pub" id="pub"/></td>
</tr>
<tr>
<td><label>Price:</label></td>
<td><input type="text" name="price" id="price"/></td>
</tr>
<tr>
<td><label>Category:</label></td>
<td><input type="text" name="cat" id="cat"/></td>
</tr>
<tr>
<td><label>Quantity:</label></td>
<td><input type="text" name="qty" id="qty"/></td>
</tr>
<tr>
<td><label>Screenshot:</label></td>
<td><input type="file" name="pic" id="pic"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="add" id="add" value="Update"/></td>
</tr>
</table>
</form>
</body>
</html>
//editbooks.jsp
在此处插入标题
书名:
作者:
出版物:
价格:
类别:
数量:
截图:
我有另一个页面“admin.jsp”,在那里我发送值id
<td><a href="editbooks.jsp?id=<%= id %>">Edit</a></td>
当我单击Edit时,我会在url上显示正确id的editbooks.jsp页面
例如:http://localhost:8082/bookstore/editbooks.jsp?id=4
但仍然不工作。您使用从请求参数获得的
where bid=?
更新数据库:
String id=request.getParameter("id");
但您没有发送任何名为“id”的参数。因此,sql语句不满足要求,并且没有更新任何记录
我想您应该已经将您的id存储在请求范围中了。如果是,则应使用隐藏字段将值存储在表单中:
<input type="hidden" name="id" value="${id}" />
我已将参数“id”从admin.jsp页传递到editbooks.jsp页。请帮助查找问题。请参阅我的更新,您可以使用${param['id']}获取参数。这应该行得通
<input type="hidden" name="id" value="${param['id']}" />