0

I have two tables in mysql database, one for student and one for teacher. Both tables have the same columns.

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?

enter image description here

index.jsp

<form  method="GET " action="statement2.jsp" autocomplete="on"> 
  <input type="radio" name="gender" value="Teacher" checked/> Teacher
  <input type="radio" name="gender" value="Student"/>Student
  <input type="submit" value="Login" /> 
  <input type="button" onclick="location.href='reg.jsp';" value="Sign Up" />
</form>

statement2.jsp

<%
   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);
%>

After Correction statement2.jsp

<%
   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); 
  %>

What wrong in this query.Why this query insert data in a only a table of student

index.jsp

   <input type="radio" name="gender" value="teacher" checked/> Teacher
   <input type="radio" name="gender" value="Student"/>Student

statement2.jsp

   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);
Rajpoot RANA
  • 13
  • 1
  • 4
  • This code has inserted data in only one table teacher i want to insert data in student if user select radio button of student what will be code for that? – Rajpoot RANA Jul 30 '16 at 22:06

1 Answers1

0

Just choose an appropriate table and then pass it to your query statement with

<%
  String gender = request.getParameter("gender");
  if (gender != null) {
    String table = gender.equals("teacher") ? "teacherTable" : "studentTable";
    // replace dots with your values
    String query = "INSERT INTO " + table + "(...) VALUES (...)";
    st.executeUpdate(query); 
  }
%>

Note that it's not a best choice to redirect to another jsp and use scriplets there (which are discouraged by specification) to process submitted data. I would rather recommend you to submit form to a Servlet and write all Java code there.

Also you may want to take a look at PreparedStatement in preference to Statement.

Check these answers if you are interested

How to avoid Java code in JSP files?

When should we use a PreparedStatement instead of a Statement?

Community
  • 1
  • 1
mr.tarsa
  • 6,386
  • 3
  • 25
  • 42