复选框(JSP PAGE)并插入到mysql数据库
Checkbox (JSP PAGE) and insert on mysql database
我有一个JSP页面,用户在表单中必须给出一个目的地:城市,国家,url_of_city和3个带有目的地类型的复选框(夏季,冬季,圣诞节),他最多可以选中2个框,所以我需要获取结果并将它们插入数据库,以便以后能够搜索此目的地, 我该怎么做?
MySQL 代码 :
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 表单代码:
<%@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 代码:
<%@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>
一个非常简单的解决方案是为您的复选框分配一个二进制值。因此,您可以使用 1,2,4 而不是 1,2,3。然后,您将对所选框的值求和并保存此唯一值。
就像在你的代码中一样:
'输入类型="复选框" 名称="ID5" 值="1" (...)` '输入类型="复选框" 名称="ID6" 值="2" (...)` '输入类型="复选框" 名称="ID7" 值="4" (...)`
下面的真值表显示了不同的值。
id: ID5 ID6 ID7 总和值:1 2 4 x 1 x 2 x x 3 x 4 x x 5 x x 6
该解决方案与SQL无关,但坦率地说,问题也不是......祝你好运!
相关文章:
- 可以't通过JQUERY传递PHP变量并将它们插入MYSQL
- 全日历插件的功能无法正确插入 mysql 表
- 从Javascript中调用PHP脚本,将数据插入mysql数据库
- 我正在尝试将vaues从ajax发送到wcf,以插入mysql表中
- 如何将动态添加的文本框插入MySQL数据库
- 使用AJAX/PHP让HTML按钮将数据插入MySQL数据库
- 使用Ajax插入Mysql表单php,无需重新加载页面
- 使用javascript和ajax插入mysql数据
- 如何在 php 中验证后将注册页面数据插入 mysql 表中
- 如何从 HTML 表将数据插入 mysql 数据库
- 如何在插入 mysql 之前验证字符串
- 当新数据插入 MySQL 时,AJAX 通知警报弹出或发出声音
- 无法使用 Node.js 将数据插入 mysql
- 在跨度单击时 - 通过页面刷新将行插入 MySQL
- 使用 jquery add_function创建的多个输入数据插入 mysql
- 使用 Angular/PHP 将 JSON 对象插入 MySQL 表
- 检查行是否已解析并插入MySQL数据库
- 通过按钮插入MySQL
- 如何在记录插入Mysql数据库后重定向到新的Html页面
- 可以't在使用node js将动态字段值插入mysql DB时,在发送头之后设置头