簡體   English   中英

添加行並運行多個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具有的類替換您的some-class-of-your-button類。 該按鈕必須在表格外才能起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM