0
<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>

In the while loop I get an array for input name="nrBucati[]" and input name="codProdus[]".

I have the query:

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


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

    // set parameters and execute
    foreach( $_POST['nrBucati'] as $bucata ) {
    return $bucata; 
    }

    foreach( $_POST['codProdus'] as $cod ) {
    return $cod;
    }

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

I cannot manage to take all the input array values through $_POST. Detailed in:

While loop - Only one input from many others is sending a value through POST

How to get each input value from the arrays nrBucati[] and codProdus[] from HTML, through POST?

Community
  • 1
  • 1
Bogdan C
  • 389
  • 1
  • 4
  • 17
  • I think I need to combine the 2 foreach into one and execute the query inside it. – Bogdan C Apr 11 '17 at 16:58
  • Why are your input names arrays? You should only use that for checkbox type inputs where you can have multiple possible values per 1 input name. For text/hidden fields you should just use a string. – Ryan Tuosto Apr 11 '17 at 16:58
  • 2
    I'm not entirely sure what you're trying to do here, but you can't use the `return` keyword to return multiple values from a loop.... – WillardSolutions Apr 11 '17 at 16:58
  • If I do not use arrays for names, I will get only one input value, instead off all. See here: http://stackoverflow.com/questions/43349889/while-loop-only-one-input-from-many-others-is-sending-a-value-through-post – Bogdan C Apr 11 '17 at 16:59
  • @EatPeanutButter You are right. I realise now. – Bogdan C Apr 11 '17 at 17:00
  • @RyanTuosto I have multiple possible values per 1 input name there. – Bogdan C Apr 11 '17 at 17:02
  • returns are usually for classes and/or methods in a case like this; are you using any? – Funk Forty Niner Apr 11 '17 at 17:12
  • Anyway, I should use an implode or foreach in the same time on both arrays because the query need both. But not sure how to do it. – Bogdan C Apr 11 '17 at 17:16
  • Thank you for your time and help! – Bogdan C Apr 11 '17 at 17:24

2 Answers2

0

Something like this to properly assign/pair up your two params and then execute your query call from within the loop.

foreach( $_POST['nrBucati'] as $id => $bucata ) {
    $cod = $_POST['codProdus'][$id];

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

Run a foreach and prepare your data inside foreach loop:

// Get posted data and execute
foreach( $_POST['nrBucati'] as $key=>$bucata ) {
    $cod = $_POST['codProdus'][$key]; // For object change this to $_POST['codProdus']->$key;

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

    if (!$stmt3->execute()){

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

    $stmt3->close();

}
Rafiqul Islam
  • 1,636
  • 1
  • 12
  • 25