【问题标题】:How to insert values from a multi row form into a database?如何将多行表单中的值插入数据库?
【发布时间】:2020-01-28 04:11:41
【问题描述】:

我创建了一个 html 表单,用户可以自己添加额外的行。链接中的网站图片。 html form site

每次用户添加新行时,都会为输入的每一行生成一个新名称。 例如。第 1 行输入名称将是:itemid0,amount0。第 2 行输入名称将是 item1,amount1。等等。下面是html表单的代码。

<?php
include('session.php');
?>

<?php 
$ItemID = "ItemID";
$ItemName = "ItemName";
$UnitPrice = "UnitPrice";

$sql = "SELECT ItemID,ItemName,UnitPrice FROM Item";
$result = $db->query($sql);
?> 
<html>
<head>
    <title>Add Sales</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="text-center">
        <h1>Add new Sales Record</h1>
    </div>
    <div class="container">
        <a href="sales.php"><button  class="w3-button w3-black w3-round-large">Back</button></a>
    </div>
    <div class="container" align="right">
    <table class="table-sm">
        <tbody>
            <tr class="table-primar">
                <th scope="col" style="padding-right: 50px;">ItemID</th>
                <th scope="col" style="padding-right: 50px">ItemName</th>
                <th scope="col">UnitPrice</th>
            </tr>
        
            <?php 
            while($rows = $result->fetch_assoc()) { 
                ?>
                <tr class="contents">
                    <td><?php echo $rows[$ItemID]; ?></td>
                    <td><?php echo $rows[$ItemName]; ?></td>
                    <td><?php echo $rows[$UnitPrice]; ?></td>
                </tr>
                <?php   
            } 
            ?> 
        </tbody>
    </table>
</div>
    <hr>
    <div class="container">
        <div class="row clearfix">
            <div class="col-md-12 column">
                <table class="table table-bordered table-hover" id="tab_logic">
                    <thead>
                        <tr >
                            <th class="text-center">
                                ItemID
                            </th>
                            <th class="text-center">
                                Amount
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <form action="addsales.php" method="POST">
                            <tr id='addr0'>
                                <td>
                                    <input type="text" name='itemid' placeholder='ItemID' class="form-control"/>
                                </select>
                                </td>
                            <td>
                                <input type="text" name='amount' placeholder='Amount' class="form-control"/>
                            </td>
                        </tr>
                        <tr id='addr1'></tr>
                    </form>
                </tbody>
            </table>
        </div>
    </div>
        <button id="add_row" class="w3-button w3-black w3-round-large" style="position: absolute; right: 130px;">New row</button>
        <button class="w3-button w3-black w3-round-large">Save</button>
</div>
<script type="text/javascript"> 
  $(document).ready(function() {
      var i = 1;
      $("#add_row").click(function() {
        $('#addr'+(i-1)).find('input').attr('disabled',true);

        $('#addr' + i).html("<td><input name='itemid" + i + "'  placeholder='ItemID' class='form-control input-md'/></td><td><input type='text' name='amount" + i + "' placeholder='Amount' class='form-control input-md'/></td>");

        $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
        i++;
    });
  });
</script>
</body>
</html>

现在的问题是,我不知道如何调用存储在表单每一行中的每个单独的信息。我在下面包含了另一个 sn-p。这就是我用于另一种只接受一行输入的表单。

<?php

include('session.php');

$itemname = trim($_POST['itemname']);
$unitprice = trim($_POST['unitprice']);
$qty = trim($_POST['qty']);
$supplierid = trim($_POST['supplierid']);

$sql = "INSERT INTO item (ItemName,UnitPrice,QtyStock,SupplierID) VALUES ('{$itemname}', '{$unitprice}', '{$qty}', '{$supplierid}')";

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Add Item</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
    <link href="asset/css/bootstrap.css" rel="stylesheet"/>
    <script src="asset/js/bootstrap.js"></script>
</head>

<body>
<div class="container">
    <div class="row">
        <div class="col-12">
            <table width="100%" cellpadding="5" cellspacing="5">
                <tr>
                    <td>
                        <h2>Adding Item</h2>
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center;">
                        <?php
                            // If insert data successfully, show information and redirect to index.php
                            if($db->query($sql) == TRUE){
                                echo "<div id='message'>New record created successfully <hr/>Please wait to redirect...</div>";
                                echo "<meta http-equiv='refresh' content='10;url=stocks.php'>";
                        } else {
                                echo "<div id='message'>Error: " . $sql . "<br>" . $db->error ."<hr/>Please wait to redirect...</div>";
                                echo "<meta http-equiv='refresh' content='10;url=stocks.php'>";
                        }
                        ?>
                    </td>
                </tr>
            </table>
        </div>
    </div>
<?php $db->close(); ?>
</div>
</body>
</html>

我想你会使用类似的东西,但以某种方式遍历所有行?

【问题讨论】:

    标签: javascript php html sql


    【解决方案1】:

    注意:只需使用 name='itemid' 中的 name='itemid[]' 和 name='amount' 中的 name='amount[]'

    <tbody>
        <form action="addsales.php" method="POST">
        <tr id='addr0'>
            <td>
               <input type="text" name='itemid[]' placeholder='ItemID' class="form-control"/>
            </td>
            <td>
               <input type="text" name='amount[]' placeholder='Amount' class="form-control"/>
            </td>
        </tr>
        </form>
    </tbody>
    
    <script type="text/javascript"> 
      $(document).ready(function() {
          var i = 1;
          $("#add_row").click(function() {
            $('#addr'+(i-1)).find('input').attr('disabled',true);
    
            $('#addr' + i).html("<td><input name='itemid[]" + i + "'  placeholder='ItemID' class='form-control input-md'/></td><td><input type='text' name='amount[]" + i + "' placeholder='Amount' class='form-control input-md'/></td>");
    
            $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
            i++;
        });
      });
    </script>
    
    <?php 
    for($i = 0; $i < count($_post['itemid']); $i++ ){
       $itemId = $_post['itemid'][$i];
       $amount = $_post['amount'][$i];
      //  here your insert query
      $sql = "INSERT INTO item (itemid,amount) VALUES ($itemId, $amount)"; // example here
      mysql_query($dbConn, "INSERT INTO item (itemid,amount) VALUES ($itemId, $amount)");
    } 
    ?>
    

    【讨论】:

    • 感谢您的回复。您能告诉我如何使用这种方法调用发送到其他页面的值吗?
    • 在表单中使用 提交按钮,例如:
    • 你好,我试过你的方法,一排效果很好。但是当我向它添加第二行时,它不断出现 Undefined itemid
    • 您好,感谢您的回复。我已经解决了这个问题。该错误是由 javascript 插入 itemid[]1、itemid[]2 等而不是 itemid[1]、itemid[2] 等引起的。
    猜你喜欢
    • 2013-10-05
    • 2023-03-06
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2011-07-07
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多