簡體   English   中英

如何從具有多行多行的單個表單中插入多個同名數據

[英]How to insert multiple data of the same name from single form with multiple row of multiple form

我有這段代碼可以很好地處理單個輸入數據,所以這是我使用的 index.php

<form method="post" id="insert_form">
        <div class="table-repsonsive">
          <span id="error"></span>
          <table class="table table-bordered" id="item_table">
            <thead>
              <tr>
                <th>Enter Item Name <td><select name="item_name[]" class="form-control item_name"><option value="">Select Category</option><?php echo fill_select_boxing($connect, "10"); ?></select></td></th>
                </tr>
                <tr>
                <th>Category</th>
                <th>Sub Category</th>
                <th><button type="button" name="add" class="btn btn-success btn-xs add"><span class="glyphicon glyphicon-plus"></span></button></th>
              </tr>
            </thead>
            <tbody></tbody>
          </table>
          <div align="center">
            <input type="submit" name="submit" class="btn btn-info" value="Insert" />
          </div>
        </div>
      </form>

並插入.php

<?php

if(isset($_POST["item_category"]))
{
 include('database_connection.php');

 for($count = 0; $count < count($_POST["item_category"]); $count++)
 {
  $data = array(
   ':item_name'   => $_POST["item_name"][$count],
   ':item_category_id'  => $_POST["item_category"][$count],
   ':item_sub_category_id' => $_POST["item_sub_category"][$count]
  );

  $query = "
   INSERT INTO mapping   
       (item_name, item_category_id, item_sub_category_id) 
       VALUES (:item_name, :item_category_id, :item_sub_category_id)
  ";

  $statement = $connect->prepare($query);

  $statement->execute($data,);
 }

 echo 'ok';
}

?>

當我嘗試在我的 web 中添加多個類別輸入時,代碼只是將第一行插入到 mysql

您可以在 for 循環中綁定您的變量,然后執行。 還有一些選項可以添加到 bindParam 以考慮不同的數據類型。 在 PHP 聯機文檔中查找 bindParam 選項。

if(isset($_POST["item_category"]))
{
 include('database_connection.php');

 $query = "
   INSERT INTO mapping   
       (item_name, item_category_id, item_sub_category_id) 
       VALUES (:item_name, :item_category_id, :item_sub_category_id)
  ";

  $statement = $connect->prepare($query);

 for($count = 0; $count < count($_POST["item_category"]); $count++)
 {
 $statement->bindParam(':item_name', $_POST["item_name"][$count]);
 $statement->bindParam(':item_category_id', $_POST["item_category"][$count]);
 $statement->bindParam(':item_sub_category_id', $_POST["item_sub_category"][$count]);
  } // end for
  
$statement->execute();

 echo 'ok';
} // end if

暫無
暫無

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

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