0

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.

Bogdan C
  • 389
  • 1
  • 4
  • 17
  • Wow, you understood very fast my issue. Thanks for reply. I forgot I must use something like name="codProdus[]". I will search into it. – Bogdan C Apr 11 '17 at 15:19
  • Thanks. Please post an answer in order to select it. It s good for your reputation :D. – Bogdan C Apr 11 '17 at 15:22

1 Answers1

0

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

Community
  • 1
  • 1
JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in C:\wamp64\www\includes\functions\fisa-init.php on line 196. What this mean? – Bogdan C Apr 11 '17 at 15:35
  • @BogdanC `$_POST['codProdus']` is an array. – JustOnUnderMillions Apr 11 '17 at 15:36
  • I switched to name="codProdus[]" and name="nrBucati[]" in html. – Bogdan C Apr 11 '17 at 15:36
  • You can do an `implode(', ',$_POST['codProdus'])` to get a string from that that looks like: `a, b, c` what was before an array like `array('a','b','c')` or you have to loop `foreach($_POST['codProdus'] as $codProducs)` and write foreach entry an entry into the database. depends on the logic you have. But for that better open a new question on SO – JustOnUnderMillions Apr 11 '17 at 15:37