2016-12-28 15 views
0

下の画像では、ユーザーが[送信]ボタンを押すと、選択した番号をデータベースに挿入します。しかし、それはactivityid番号には機能しません。 Submitボタンを押すと、activityidはnullを返し、buyは選択した番号を返します。どのように私はenter image description hereデータの取得とデータベースへの挿入

music.jsp

<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> 

         <% 
          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()) { 

           out.println("<tr>"); 
           out.println("<td>" + rs.getString("id")+"</td>"); 
           out.println("<td>" + rs.getString("type")+ "</td>"); 
           out.println("<td>" + rs.getString("description")+ "</td>"); 
           out.println("<td>" + rs.getString("city") + "</td>"); 
           out.println("<td>" + rs.getString("location")+ "</td>"); 
           out.println("<td>" + rs.getString("date") + "</td>"); 
           out.println("<td>" + rs.getString("price") + "</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(); 

         %> 

Reservation.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ page import ="java.sql.*" %> 
<% 

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 ('" + request.getParameter("id") + "', '" + username + "','" + request.getParameter("buy") + "') "; 

java.sql.Statement st = con.createStatement(); 

int rowNum = st.executeUpdate(sorgu); 
response.sendRedirect("paypal.html"); 
st.close(); 
%> 
+0

私が表示されていないフォームタグを整理すると、フォーム送信で「ID」を含める方法方法以外のすべてを排除しましたActivityId列をパラメータとして渡します。 –

+0

はい。これが問題です。どうやってやるの ?私はGoogleで検索したが、icannotは見つけた。これは私の解決策ですあなたは私を助けることができますか? – tripley

答えて

1

あなたは、各行内のすべてのデータの周囲のタグを取得し、追加する必要があります(データベース上で正しいのActivityIDを挿入行うことができます非表示)のフォームタグを各列に追加し、すべてのデータがフォームの提出に含まれるようにします。明確にするために、私はあなたに名前を割り当てた

     rs = st.executeQuery("select * from activities where type='müzik'"); 
         while (rs.next()) { 

          out.println("<tr>"); 
          out.println("<form action='reservations.jsp'>"); 
          out.println("<td>" + rs.getString("id") + "<input type='hidden' name='id' value='" + td.getString("id")+"'></td>"); 
          out.println("<td><input type='submit' value='Submit'></form></b></td>"); 
          out.println("</td></tr>"); 

         } 
         st.close(); 
+0

あなたの言ったことはしましたが、データベースのアクティビティIDがnullのまま返されます – tripley

+0

申し訳ありませんが、入力フィールドに「name = 'id'」を追加するのを忘れていました。私は私の答えを訂正しました。しかし、db insert jspページに提出されたパラメータやデータベースのデータから値が利用できないという問題がありますか?それはパラメータ付きですよね? – ControlAltDel

+0

ありがとうございます – tripley

関連する問題