2016-12-28 17 views
1

jspページテーブルから値を取得し、reservation.jspを使用してデータベースを挿入したいとします。以下のコードでは、テーブルの "Buy"という名前のオプション部分を取得することができますが、データベースのプリントアウトができますが、テーブルのactivityId部分を取得できません。データベースにnullを返します。 reservations.jspでは、actvityId1は読まないでください。私は問題が "name = 'buy'"のようなコードを含まないactivityid1部分にあると思う。 activityId1の値を取得するにはどうすればよいですか? jsp pagejspページテーブルからの値の取得

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(); 
%> 
+0

助ける

request.getParameter("activityid1Hidden"); 

希望?それはフォームの 'submit'ですか? – ItamarG3

+0

おそらくあなたの問題には関係しませんが、コードはSQLインジェクションに対して脆弱です。代わりに 'PreparedStatement'を使用してください。最初のコードは – GurV

+0

です。私はやりますが、これは今より重要です:) – tripley

答えて

0

activityid1は、フォームの一部ではありません。その値はリクエストとともに送信されません。

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'> <input type=\"hidden\" id=\"activityid1Value\" name=\"activityid1Hidden\" value=\""+activityid1 +"\"></form></b>"); 

そして、それを取得する:あなたは、フォームのように隠された要素を追加必要私はあなた、あなたが `buy`ボタン入力を作成している

関連する問題