1
我想從jsp頁表中獲取值,並使用reservations.jsp插入它的數據庫。下面的代碼我可以在表中找到名爲「購買」的選項部分正確打印數據庫,但我無法在表中獲取activityId部分。它在數據庫中返回null。在reservations.jsp中,不要讀取actvityId1。我認爲問題出在activityid1部分,代碼不包括像「name ='buy'」這樣的代碼。我如何獲得activityId1值? 從jsp頁表獲取值
music.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg">
<form method="post" action="reservations.jsp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Book Ticket</title>
</head>
<center>
<table border="1" width="30%" height="30%">
<th><font color='#D18603'>ActivityID</font>
<th><font color='#D18603'>Type</font></th>
<th><font color='#D18603'>Description</font></th>
<th><font color='#D18603'>City</font></th>
<th><font color='#D18603'>Location</font></th>
<th><font color='#D18603'>Date</font></th>
<th><font color='#D18603'>Price</font></th>
<th><font color='#D18603'>Buy</font>
<form action="some.jsp">
</tr>
<form method="post">
<%
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from activities where type='müzik'");
while (rs.next()) {
String activityid1 = rs.getString("id");
String type1 = rs.getString("type");
String description1 = rs.getString("description");
String city1 = rs.getString("city");
String location1 = rs.getString("location");
String date1 = rs.getString("date");
String price1 = rs.getString("price");
out.println("<tr>");
out.println("<td>" + activityid1 + "</td>");
out.println("<td>" + type1 + "</td>");
out.println("<td>" + description1 + "</td>");
out.println("<td>" + city1 + "</td>");
out.println("<td>" + location1 + "</td>");
out.println("<td>" + date1 + "</td>");
out.println("<td>" + price1 + "</td>");
out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>");
out.println("</tr>");
}
st.close();
%>
</center>
</table>
<tr>
<td><input type="reset" value="Reset" /></td>
</tr>
</form>
<br><br><a href='logout.jsp'>Log out</a>
</form>
</body>
</html>
reservations.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%
request.getParameter("activityid1");
request.getParameter("buy");
String username = (String) request.getSession().getAttribute("username");
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");
String sorgu = "INSERT INTO reservation(id,username,buy) VALUES ('" + activityid1 + "', '" + username + "','" + request.getParameter("buy") + "') ";
java.sql.Statement st = con.createStatement();
int rowNum = st.executeUpdate(sorgu);
response.sendRedirect("paypal.html");
st.close();
%>
你在哪裏創建'buy'按鈕輸入?它是表單的「提交」嗎? – ItamarG3
可能與您的問題無關,但代碼易受SQL注入攻擊。改用'PreparedStatement'。第一個代碼中的 – GurV
。我會做,但現在更重要:) – tripley