我想在数据库的单列中使用JSP存储多个复选框值,但在单行中使用单值。 我的JSP代码QuickFunction.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2 align="center"> Quick Function</h2>
<form action="QuickServlet">
<table border="1" align="center">
<tr><td width=50 align="center">1</td>
<td>Book A voucher</td>
<td>Sales Voucher</td>
<td><input type="checkbox" name="voucher"
value="Sales Voucher" /></td>
</tr>
<tr><td></td><td></td>
<td>Purchase Voucher</td>
<td><input type="checkbox" name="voucher"
value="Purchase Voucher" /></td>
</tr>
<tr><td></td><td></td>
<td>Receipt</td>
<td><input type="checkbox" name="voucher"
value="Receipt" /></td>
</tr>
<tr><td></td><td></td>
<td>Payment </td>
<td><input type="checkbox" name="voucher"
value="Payment" /></td>
</tr>
<tr>
<td></td><td></td><td> Contra </td>
<td><input type="checkbox" name="voucher"
value="Contra" /></td>
</tr>
<tr><td></td><td></td>
<td> Journal </td>
<td><input type="checkbox" name="voucher"
value="Journal"></td>
</tr>
</table>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
我的servlet代码QuickServlet(doGet方法):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String voucher = "";
String a[]=request.getParameterValues("voucher");
for(int i=0;i<a.length;i++){
voucher+=a[i]+"";
}
DBConnection dbc=new DBConnection();
Connection con=dbc.getNewConnection();
Statement st = null;
ResultSet rs = null;
try
{
st = con.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}
String updatecust = "insert into vouchers(voucher) values('"+voucher+"');";
try
{
int i=st.executeUpdate(updatecust);
if(i>0){
out.print(" saved successfully....!!");
}
else
out.print("Sorry..!! Got an exception.");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
这会将复选框值存储在单行的单行中,如下所示:
mysql> select * from vouchers;
+--------------------------------------+
| voucher |
+--------------------------------------+
| Sales VoucherPurchase VoucherReceipt |
+--------------------------------------+
但我想在一行中存储一个值,依此类推。如下(预期产出):
mysql> select * from vouchers;
+------------------+
| voucher |
+------------------+
| Sales Voucher |
| Purchase Voucher |
| Receipt |
+------------------+
请帮帮我。在此先感谢!!
答案 0 :(得分:0)
在您的代码中,首先将您使用for循环然后插入所有凭证连接起来。
如果您希望以行方式插入每个凭证,而不是在for循环中插入连接凭证,则使用for循环调用插入查询并插入凭证。
像这样:
for(int i=0;i<a.length;i++){
String updatecust = "insert into vouchers(voucher) values('"+voucher+"');";
}
答案 1 :(得分:0)
只需检查创建两个字段(id,凭证)的小表(multiheck)即可。
<?php include('config.php'); ?>
<?php
$voucher= $_POST['voucher'];
if($_POST['Submit']=='Submit'){
for($i=0;$i<sizeof($voucher);$i++){
$statement = $db->prepare("INSERT INTO multicheck (voucher) VALUES (?)");
$statement->execute(array($voucher[$i]));
}
}
?>
这是表格:
<form action="actionform.php" method="POST"> <input type="checkbox" value="Sales Voucher" name="voucher[]">Sales Voucher<br /> <input type="checkbox" value="Receipt" name="voucher[]">Receipt<br /> <input type="checkbox" value="Contra" name="voucher[]">Contra<br /> <input type="checkbox" value="Journal" name="voucher[]">Journal<br /> <input type="submit" name="Submit" value="Submit"> </form>