如何插入 MySQL 多行 PHP

How insert to MySQL many rows PHP

我想向 mysql 发送很多行。 我有一个 php 表格,其中列出了所有产品(大约 200 种)。我不知道如何将所有项目添加到 mysql 数据库中... 是否需要循环?

<form method="POST" action=""; model_load('orderForm_model', 'sendOrder'); echo "">
    <div class="col-1">PLU</div>
    <div class="col-1">IL</div>
    <div class="col-2">OP</div>>";
    
    $orderStandardForm = $this->__db->execute("SELECT ID_product from product");

    foreach($orderStandardForm as $orderStandardForm_)
    {
    echo "  <div class="row">
                <div class="col-1"><input type="number" name="plu" value="{$orderStandardForm_['ID_produkt']}"></div>
                <div class="col-1"><input type="number" pattern="[0-9]*" inputmode="decimal" id="input1" step="0.01" name="il" class="width100"></div>
                <div class="col-2"><input type="text" id="opisTxt_{$orderStandardForm_['ID_produkt']}" name="op" class="width100"></div>
            </div>";
    }
    echo "   <input class="btn btn-primary width100" type="submit" value="Send order">
</form>";

我无法理解你的问题。对吗?

the code name="plu" change to name="plu[]"

the code name="il" change to name="il[]"

the code name="op" change to name="op[]"

试试这个:

HTML:

        <form method="POST" action=""; model_load('orderForm_model', 'sendOrder'); echo "">
        <div class="col-1">PLU</div>
        <div class="col-1">IL</div>
        <div class="col-2">OP</div>>";
        
        $orderStandardForm = $this->__db->execute("SELECT ID_product from product");

        foreach($orderStandardForm as $orderStandardForm_)
        {
        echo "  <div class="row">
                    <div class="col-1"><input type="number" name="plu[]" value="{$orderStandardForm_['ID_produkt']}"></div>
                    <div class="col-1"><input type="number" pattern="[0-9]*" inputmode="decimal" id="input1" step="0.01" name="il[]" class="width100"></div>
                    <div class="col-2"><input type="text" id="opisTxt_{$orderStandardForm_['ID_produkt']}" name="op[]" class="width100"></div>
                </div>";
        }
        echo "   <input class="btn btn-primary width100" type="submit" value="Send order">
    </form>";

PHP保存过程:

        $plu_arr = $_POST["plu"];
    $il_arr = $_POST["il"];
    $op_arr = $_POST["op"];
    $row_index = 0;
    $row_arr = array();
    foreach($plu_arr as $plu_data){
       if(isset($il_arr[$row_index]) && $il_arr[$row_index] != ''){
        $row_arr[] = "('".$plu_data."','".$il_arr[$row_index]."','".$op_arr[$row_index]."')";
        }
        $row_index++;
    }

    $ins_qry = "INSERT INTO your_table_name (plu, il, op) VALUES ".implode(", ", $row_arr);
    $db_ins = $this->__db->execute($ins_qry);