Home > Software design >  How insert to MySQL many rows PHP
How insert to MySQL many rows PHP

Time:12-15

I'd like to send a lot of rows to mysql. I have a php form that lists all the products (about 200). I don't know how to add all the items to the mysql database ... Is a loop necessary?

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

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

CodePudding user response:

Try this:

HTML:

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

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

PHP save process:

        $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);

CodePudding user response:

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[]"
  • Related