2017-04-11 116 views
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> 

在while循环中,我得到一个数组input name="nrBucati[]"input name="codProdus[]"

我有查询:

$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(); 

我不能管理通过$_POST采取所有输入数组值。详见:

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

如何从阵列nrBucati[]和HTML codProdus[],通过POST每个输入值?

+0

我想我需要到2的foreach合并成一个,并执行里面的查询。 –

+0

为什么你的输入名称数组?您只能将其用于复选框类型的输入,其中每个输入名称可以有多个可能的值。对于文本/隐藏字段,您应该只使用一个字符串。 –

+2

我不完全确定你要在这里做什么,但是你不能使用'return'关键字从一个循环中返回多个值...... – WillardSolutions

回答

0

像这样正确分配/配对你的两个参数,然后从循环内执行你的查询调用。

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; 
     } 
} 
+0

大声笑。像魅力一样工作!你救了我的屁股!我喜欢你。感谢您理解我可怜的解释! –

+0

我知道我不得不把这两个参数结合起来,但不知道该怎么做。 –

0

运行一个foreach并准备内部foreach回路数据:

// 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(); 

}