添加行并运行多个mySQL查询


Adding rows and Running Multiple mySQL queries

我正在想办法解决这个问题。。我需要能够使用JS将表行与输入字段附加在一起,这样就可以用同一个表单输入多个项目,但我需要以某种方式使每个输入都有一个唯一的名称来作为PHP的目标。从那里,我需要在每个数据库上运行一个insert语句,以插入到mySQL数据库中。。我有点被难住了

=================================================

   <form action="" method="post" id="myForm">
        <table class="addTable">
            <tr>
                <th>Title</th>
                <th>Type</th>
                <th>Quantity</th>
                <th>Customer Price</th>
            </tr>
            <tr>
                <td>
                    <input type="text" name="name" placeholder="Title" maxlength="60" value="<?php echo $_POST['name']; ?>">
                </td>
                <td>
                    <input type="text" name="type" placeholder="Type" maxlength="20" value="<?php echo $_POST['type']; ?>">
                </td>
                <td>
                    <input type="number" name="qty" placeholder="QTY" max="100" value="<?php echo $_POST['qty']; ?>">
                </td>
                <td>
                    <input type="text" name="price" placeholder="Price" value="<?php echo $_POST['price']; ?>">
                </td>
            </tr>
        </table>
    </form>

您可以使用数组作为名称,例如:

   <form action="" method="post" id="myForm">
        <table class="addTable">
            <tr>
                <th>Title</th>
                <th>Type</th>
                <th>Quantity</th>
                <th>Customer Price</th>
            </tr>
            <tr>
                <td>
                    <input type="text" name="name[]" placeholder="Title" maxlength="60" value="<?php echo $_POST['name']; ?>">
                </td>
                <td>
                    <input type="text" name="type[]" placeholder="Type" maxlength="20" value="<?php echo $_POST['type']; ?>">
                </td>
                <td>
                    <input type="number" name="qty[]" placeholder="QTY" max="100" value="<?php echo $_POST['qty']; ?>">
                </td>
                <td>
                    <input type="text" name="price[]" placeholder="Price" value="<?php echo $_POST['price']; ?>">
                </td>
            </tr>
        </table>
    </form>

然后你可以使用类似的东西:

<?php
for($i = 0; $i < count($_POST['name']; $i++) {
  $name = $_POST['name'][$i];
  $type = $_POST['type'][$i];
  $qty = $_POST['qty'][$i];
  $price = $_POST['price'][$i];
  // make your query here
}

还有一个小建议:在html代码中,不要直接回显$_POST值,首先检查它们是否存在,例如<?= isset($_POST['price'])?$_POST['price']:""?>

<?=<?php echo 的快捷方式

isset($_POST['price'])?$_POST['price']:""部分是if-else语句的快捷方式。(称为三元算子)

意思与此相同:

if(isset($_POST['price'])) {
  echo $_POST['price'];
} else {
  echo "";
}

isset()本身检查变量(在这种情况下是数组键)是否存在。

编辑:关于附加的更新信息

如果你能使用jQuery,它很简单。如果你不使用它,就开始使用它:)

<script>
$(document).ready(function() {
  $('.some-class-of-your-button').click(function() {
    $('.addTable').append($('.addTable tr:nth-child(2)').clone());
    $('.addTable tr:last-child input').val("");
  });
});
</script>

some-class-of-your-button替换为按钮所具有的类。按钮必须在表外才能工作。