如何使用 jsp 在数据库中的单列中插入多个复选框值,在单行中插入一个值



我想在数据库中使用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>

My servlet code 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          |
+------------------+

请帮助我。提前感谢!!

在您的代码中,首先将所有凭证连接到使用 for 循环,然后插入。

如果您希望按行插入每个凭证,则不要在 for 循环中串联凭证,而是使用 in for 循环调用插入查询并插入凭证。

喜欢这个:

   for(int i=0;i<a.length;i++){
       String updatecust = "insert into vouchers(voucher) values('"+voucher+"');";
    }
只需

创建一个包含两个字段(id,凭证)的小表(多重检查)即可。

<?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>

最新更新