简体   繁体   English

如何插入 MySQL 多行 PHP

[英]How insert to MySQL many rows PHP

I'd like to send a lot of rows to mysql.我想向 mysql 发送很多行。 I have a php form that lists all the products (about 200).我有一个 php 表格,列出了所有产品(大约 200 个)。 I don't know how to add all the items to the mysql database... Is a loop necessary?我不知道如何将所有项目添加到 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>";

I'm having trouble understanding your question.我很难理解你的问题。 Is that correct?那是对的吗?

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

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

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

Try this:尝试这个:

HTML: 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 save process: 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){
        $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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM