How to save data on database from a dynamic table

How to save data on database from a dynamic table. I am able to display the invoices on the table, I would like to save individual rows only but instead, the table saves only the first reference(invoice) and the last amount

        <thead>
            <tr>
                <th class="text-md-left"><?php echo lang("nьз╕"); ?></th>
                <th class="text-md-left"><?php echo lang("date"); ?></th>
                <th class="text-md-left"><?php echo lang("invoice"); ?></th>
                <th class="text-md-right"><?php echo lang("amount"); ?></th>
                <th class="text-md-right"><?php echo lang("amount_due"); ?></th>
                <th class="text-md-right"><?php echo lang("payments_create"); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $amount = 0; $k = 1; $sp = strlen(count($invoices)."");
            foreach ($invoices as $row) {
                if(is_object($row)){$row = objectToArray($row);} ?>
                <tr>
                    
                    <td class="text-md-left"><?php echo str_pad($k, $sp, "0", STR_PAD_LEFT) ?></td>
                    <td class="text-md-left"><?php echo date_MYSQL_PHP($row["date"]) ?><small><small><br>Date Due <?php echo $row['date']; ?></small></small></td>
                    <td class="text-md-left"><?php echo form_input ($invoice_input); echo $row["reference"],
       form_hidden('receipt[invoice_id]', set_value('receipt[invoice_id]', $invoice_id)),
       form_hidden('payment[invoice_id]', set_value('payment[invoice_id]', $invoice_id));
      ?></td>
                    <td class="text-md-right"><?php echo formatMoney($row["total"], $currency); ?></td>
                    <td class="text-md-right"><?php echo formatMoney($row["total_due"], $currency); ?></td>
                    <td class="text-md-right"> 
        <input type="number" step="any" min="0" value="<?php foreach($row['payment'] as $payment)
    echo set_value("payment[amount]<br>", $invoice->total_due);  echo str_pad($k, "total_due") ?>" name="payment[amount]" class="form-control" />
     
     </td>
        
      
    </div></tr>
            <?php
                $k++;
                $amount += $row["amount"];
            } // end foreach
            ?> ```