While loop - Only one input from many others is se

2019-09-14 18:07发布

This is the code where I get my input names and values from a table called optionale and doing something with these:

<form role="form" autocomplete="off" action="includes/functions/fisa-init.php" method="POST">
<?php
   connectDB();
   $query = mysqli_query($mysqli, "SELECT * FROM `optionale`") or die(mysqli_error($mysqli));
   while($row = mysqli_fetch_array($query))
   { 
?>
   <span><?php echo $row['denumire']; ?></span>
   <input type="text" name="nrBucati">
   <input type="hidden" value="<?php echo $row['cod']; ?>" name="codProdus">
<?php } ?>
</form>

The optionale table looks like this:

Optionale table

The HTML looks like this:

HTML look

As you can see in the last picture, I have in HTML, a name for input (taken from optionale table) and the actual input in which I write a value.

fisa-init.php:

$stmt3 = $mysqli->prepare("
            UPDATE 
            `stocuri` 
            SET 
            `cantitate` = `cantitate` - ?
            WHERE `cod` = ?
            ");


    $stmt3->bind_param("is", $bucata, $cod);

    // set parameters and execute
    $bucata = mysqli_real_escape_string($mysqli, $_POST['nrBucati']);
    $cod = mysqli_real_escape_string($mysqli, $_POST['codProdus']);

    if (!$stmt3->execute()) 
        {
            echo "Execuția a întâmpinat o eroare: (" . $stmt3->errno . ") " . $stmt3->error;
        }
    $stmt3->close();

    $mysqli->close();

In the code above (fisa-init.php) I am trying to take all the input values from my HTML and update rows in another table called stocuri:

enter image description here

As you can see, only the second row from stocuri table was updated, but I wrote values in all 5 inputs. It got only the last input.

How to modify the while loop in order to take all my inputs value?

If something is not clear, I apologize a hundred times. I will explain all the informations that are needed.

P.S. cod in table optionale is the same with cod in table stocuri. In this way, I know where to update values.

1条回答
萌系小妹纸
2楼-- · 2019-09-14 18:18

Each <input> MUST have an individual name or a named array.

So give each an aditional number like name1,name2 or use an named array like name[]

Finally this name="codProdus[]" is your solution.

Read more here HTML input arrays

Have a nice day

查看更多
登录 后发表回答