Solution 1 :

The input field names with [] such as name=”disc[]” will appear as arrays at the server.

You will need loop in your php code, extracting the values from the arrays and running several INSERTs towards the database.

Solution 2 :

You can use multi_query function that already exists. Maybe this would help you
https://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php

Something like this

if(!empty($thingOne)) {
    $sql = "YOUR QUERY";
} else if(!empty($thingTwo)) {
    $sql.= "YOUR QUERY";
} else if(!empty($thingThree)) { 
    $sql.= "YOUR QUERY";
}

Then you just execute the query with multi_query function as in PHP Manual.

This should work, haven’t tested it.

Problem :

am building a production entry form that gives machine operators the opportunity to enter multiple batches of production. Because of this, the final form can have a different number of rows upon submission depending on if the operator ran 1, 2, 3+ batches. Each form row should be submitted to an individual MySQL database table row (if there are three batches each batch should be a new row in the table).

I have added the JavaScript to add new rows to the HTML form successfully but I am having problems with getting the PHP code to submit EACH of the rows to the MySQL database. Right now only the first form row is submitting to the MySQL database. Can someone help me fix the PHP code?

I have included by code below.

Index.html

<form class="needs-validation" id="productionEntryForm" action="php/production-entry.php" method="post" novalidate>
    <div class="row">
        <input type="date" id="productionDate" name="productionDate">
        <select id="operator" name="operator"></select>
        <select id="line" name="line"></select>
    </div>
    <div id="batchContainer">
        <div class="row" id="divBatch1">
            <select id="disc1" name="disc[]"></select>
            <select id="plastic1" name="plastic[]"></select>
            <select id="color1" name="color[]"></select>
            <input id="colorUsed1" type="number" name="colorUsed[]">
            <input id="plasticUsed1" type="number" name="plasticUsed[]">
            <input id="regrind1" type="number" name="regrind[]">
            <input id="firsts1" type="number" name="firsts[]">
            <input id="seconds1" type="number" name="seconds[]">
            <input id="colorChange1" type="number" name="colorChange[]">
        </div>
    </div>
    <div class="row">
        <input id="newDisc" type="button" onclick="addDisc()" value="New Disc">
        <button type="submit">Finish Run</button>
    </div>
</form>

production-entry.php

<?php
    $productionDate = $_POST['productionDate'];
    $operator = $_POST['operator'];
    $line = $_POST['line'];
    $disc = $_POST['disc'];
    $plastic = $_POST['plastic'];
    $color = $_POST['color'];
    $colorUsed = $_POST['colorUsed'];
    $plasticUsed = $_POST['plasticUsed'];
    $regrind = $_POST['regrind'];
    $firsts = $_POST['firsts'];
    $seconds = $_POST['seconds'];
    $colorChange = $_POST['colorChange'];

    $host = "localhost";
    $dbusername = "username";
    $dbpassword = "password";
    $dbname = "testDB";

    // Create connection
    $conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
    if (mysqli_connect_error()){
        die('Connect Error (' . mysqli_connect_errno() .') ' . mysqli_connect_error());
    }
    else{
        $sql = "INSERT INTO PRODUCTION_ENTRY (PRODUCTION_DATE, OPERATOR, PRODUCTION_LINE, DISC, PLASTIC, COLOR, COLOR_USED_GRAMS, PLASTIC_USED_LBS, REGRIND_LBS, FIRSTS, SECONDS, COLOR_CHANGE)
        values ('$productionDate','$operator','$line','$disc','$plastic','$color','$colorUsed','$plasticUsed','$regrind','$firsts','$seconds','$colorChange')";
        echo $sql;
        if ($conn->query($sql)){
            echo "Data submitted successfully";
        }
        else{
            echo "Error: ". $sql ."<br>". $conn->error;
        }
        $conn->close();
    }
?>

Comments

Comment posted by Funk Forty Niner

Why are the inputs as an multi array

Comment posted by mickmackusa

else if

By