Suppose I have a application form which contain lot of boxes like email,username,age etc...I have connected this application form to database(mysql) through action="register.php" in application form...
NOw i want that in database,username and email id should be unique.Means no two users cant have same username or email id..
What I know to do This....
When you created tables in mysql then make these fields (username,email) unique(by primary key).Butif i submit the form with same username already in database then error msj will be showed on the next page something like this.username is priamry.....whatever..
Problem in this...
The error msj should be displayed on the reflected registration page of that textbox(username textbox and email textbox)..
How i can do this.Please explain with example For your ease i am giving code for application form...something like this
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
st = cn.createStatement();
String username=request.getParameter("use");
String fnm=request.getParameter("fnm");
String lnm=request.getParameter("lnm");
String eml=request.getParameter("email");
String phone=request.getParameter("phone");
String pass=request.getParameter("pwd");
st.executeUpdate("insert into student values('"+username+"','"+fnm+"','"+lnm+"','"+eml+"','"+phone+"','"+pass+"')");
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet con_reg</title>");
out.println("</head>");
out.println("<body>");
response.sendRedirect("confirm_reg");
out.println("</body>");
out.println("</html>");
}catch(Exception e){
e.printStackTrace();
response.sendRedirect("reg_page");
out.println("message");
}
}