title: choose table according to input in jsp
body: <p>I have two tables in mysql database, one for student and one for teacher. Both tables have the same columns. </p>
  
  <p>The code below inserts data only to the teacher's table. If user selects teacher's radio button, then the sign up data should be stored in teacher's table. If the user selects student's radio, then the sign up data should be stored in student's table. What will be a solution for that?</p>
  
  <p><a href="http://i.stack.imgur.com/2rrIc.jpg" rel="nofollow">enter image description here</a></p>
  
  <p><strong>index.jsp</strong></p>
  
  <pre><code>&lt;form  method="GET " action="statement2.jsp" autocomplete="on"&gt; 
    &lt;input type="radio" name="gender" value="Teacher" checked/&gt; Teacher
    &lt;input type="radio" name="gender" value="Student"/&gt;Student
    &lt;input type="submit" value="Login" /&gt; 
    &lt;input type="button" onclick="location.href='reg.jsp';" value="Sign Up" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>statement2.jsp</strong></p>
  
  <pre><code>&lt;%
     String name=request.getParameter("first");
     String abc=request.getParameter("last");
     String cde=request.getParameter("user");
     String pass=request.getParameter("password");
     String confpass=request.getParameter("confirmpass");
     String emails=request.getParameter("email");
     String months=request.getParameter("month");
     String day=request.getParameter("day");
     String year=request.getParameter("year");
     String gender=request.getParameter("gender");
  
     String Sql="insert into teacher2(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) values('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
     st.executeUpdate(Sql);
  %&gt;
  </code></pre>
  
  <p><strong>After Correction</strong>
  <strong>statement2.jsp</strong></p>
  
  <pre><code>&lt;%
     String name=request.getParameter("first");
     String abc=request.getParameter("last");
     String cde=request.getParameter("user");
     String pass=request.getParameter("password");
     String confpass=request.getParameter("confirmpass");
     String emails=request.getParameter("email");
     String months=request.getParameter("month");
     String day=request.getParameter("day");
     String year=request.getParameter("year");
     String gender=request.getParameter("gender");
  
     if (gender != null) {
     String table = gender.equals("teacher") ? "teacher2" : "student";
     // replace dots with your values
     String query = "INSERT INTO " + table + "(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) VALUES ('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
  
     st.executeUpdate(query); 
    %&gt;
  </code></pre>
  
  <hr>
  
  <p>What wrong in this query.Why this query insert data in a only a table of student</p>
  
  <p><strong>index.jsp</strong></p>
  
  <pre><code>   &lt;input type="radio" name="gender" value="teacher" checked/&gt; Teacher
     &lt;input type="radio" name="gender" value="Student"/&gt;Student
  </code></pre>
  
  <p><strong>statement2.jsp</strong></p>
  
  <pre><code>   String gender = request.getParameter("gender");
     if (gender != null) {
     String table = gender.equals("teacher") ? "teacher2" : "student";
  // replace dots with your values
     String query = "INSERT INTO " + table + "(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) VALUES ('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
    st.executeUpdate(query);
  </code></pre>