简体   繁体   中英

Checkbox (JSP PAGE) and insert on mysql database

i have a jsp page that user in form has to give a destination : city,country,url_of_city and 3 checkbox with the kind of the destination (summer,winter,christmas) , he can check max 2 boxes, so i need to take the results of this and insert them to a database in order to be able later to search for this destination , how can i do it this?

mysql code :

CREATE TABLE DEST_C(ID_DEST_C INT(5),CATEGORY VARCHAR(45))
CREATE TABLE DEST(ID1_DEST INT(6),ID2_DEST INT (6),COUNTRY VARCHAR(40),CITY VARCHAR(40),URL VARCHAR(5400));

jsp form code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>create dest</title>
    </head>
    <body>
        <html>
    <head>
        <title>CREATE DESTINATION</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1> CREATING DESTINATION </h1>
        <form name="createdest" method="get" action="create dest code.jsp">
        Country: <input type="text"  required  name="id8" /> <br>
        City: <input type="text"  required  name="id9" /> <br>
        URL Video: <input type="url"  required  name="id10" /> <br> <br>
        <i><ins>Categorize the destination (max 2): </ins></i>  <br> <br>
        <input type="checkbox" name="id5" value="1" onClick="return KeepCount()" >Christmas<br>
        <input type="checkbox" name="id6" value="2" onClick="return KeepCount()" >Winter <br>
        <input type="checkbox" name="id7" value="3" onClick="return KeepCount()" >Summer <br> <br>

        <input type="submit" value="CREATE DESTINATION" /> 


        <br>


        </form>
       <SCRIPT LANGUAGE="javascript">

function KeepCount() {

var NewCount = 0

if (document.createdest.id5.checked)
{NewCount = NewCount + 1}

if (document.createdest.id6.checked)
{NewCount = NewCount + 1}

if (document.createdest.id7.checked)
{NewCount = NewCount + 1}

if (NewCount == 3)
{
alert('Pick Just Two Please')
document.createdest; return false;
}
} 
</SCRIPT>

JSP CODE for inserts :

<%@page import="java.sql.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>create dest code</title>
    </head>
    <body>
         <%
    int m[] = new int[5000]; String s[] = new String[5000];        
   Class.forName("com.mysql.jdbc.Driver"); 
   String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234"; 
   Connection myConnection = DriverManager.getConnection(myDatabase);
   Statement myStatement = myConnection.createStatement();  
   boolean check=null!=request.getParameter("id5");
   boolean check1=null!=request.getParameter("id6");
   boolean check2=null!=request.getParameter("id7");
   String id8=request.getParameter("id8");
   String id9=request.getParameter("id9");
   String id10=request.getParameter("id10");
   String sqlString = "INSERT INTO DEST(COUNTRY,CITY,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
   myStatement.executeUpdate(sqlString);
    myStatement.executeUpdate(sqlString1);
   myStatement.close(); 
   myConnection.close(); %>
    </body>
    <h1 style="color:blue;">Successful Registration </h1>
</html>

A very simple solution would be to assign a binary value to your checkboxes. So rather than 1,2,3 you could use 1,2,4. Then you would sum the values of the selected boxes and save this unique value.

As in your code:

`input type="checkbox" name="id5" value="1" (...)`
   `input type="checkbox" name="id6" value="2" (...)`
   `input type="checkbox" name="id7" value="4" (...)`

The truth table below shows you the different values.

id:         ID5   ID6  ID7    Sum
value:       1     2    4  
             x                  1
                   x            2
             x     x            3
                        x       4
             x          x       5
                   x    x       6

The solution is not SQL related, but quite frankly, neither is the question... Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM