Skip to content
Advertisement

Insert data when selecting a dropdown option, MySQL, JAVA, JSP

well my english is not so good, but i will try to explain me, I have a question about how to make an INSERT query when selecting an option from my dropdown, I explain myself, I have a section to insert a user on my website, and an option to select a profile

enter image description here

It should be noted that my dropdown options are data from my PROFILE table

enter image description here

This is my code where I show the values ​​of my dropdown from my PROFILE table

<select class="form-control" style="width: 250px"> 
                            <option value="-1">Selecciona un perfil</option>
                            <% 
                                try
                                {
                                    String Query = "SELECT * FROM perfil";
                                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registrousuario","root", "");
                                    
                                    Statement stm = conn.createStatement();
                                    ResultSet rs = stm.executeQuery(Query);
                                    while(rs.next())
                                    {  
                                        %>
                                           <option value="<%=rs.getString("idPerfil")%>"><%=rs.getString("nombrePerfil")%></option>
                                        <%
                                    }
                                }
                                catch(Exception ex)
                                {
                                    ex.printStackTrace();
                                    out.println("Error: "+ex.getMessage());
                                }
                            %>  
                        </select>    

what I try to do in my table USER

enter image description here

is to insert the option either EMPLOYEE or PROFILE dropdown

my INSERT query is as follows

@RequestMapping(value = "agregar.htm", method = RequestMethod.POST)
    public ModelAndView Agregar(Persona p)
    {
        String sql = "INSERT INTO usuario(claveUsuario, nombre, apellido, email, contrasena, idPerfil) VALUES(?, ?, ?, ?, ?, ?)";
        this.jdbcTemplate.update(sql,p.getClave(), p.getNom(), p.getApe(), p.getCorreo(), p.getPass(), p.getIdPerfil());
        return new ModelAndView("redirect:/index.htm");
    }

I insert the other data correctly, only I need to be able to insert the values ​​of my DROPDOWN in my USER table

I don’t know if I would miss doing a JOIN to my PROFILE table in my INSERT query, in order to register the profile data to my USER table

I hope I have explained well, any help would be very important

Advertisement

Answer

You have missed the id and name of the select element. It should be as follows:

<select id="idPerfil" name="idPerfil" class="form-control" style="width: 250px"> 
8 People found this is helpful
Advertisement