我的foreach php / MySQL代码有什么问题?

时间:2021-04-10 00:08:36

I tried to insert multiple rows from one form using checkboxes, include take values from input box, all of them has the structure:

我尝试使用复选框从一个表单插入多行,包括从输入框中获取值,所有这些都具有以下结构:

<input id="Valor" type="text" name="Valor[]" value="<?php echo $row2["Valor"];  ?>" />

and the checkbox is like:

并且复选框如下:

<input type="checkbox" name="Pago[]" id="Pago" value="<?php echo $row2["IdSolicitudTarjeta"]; ?>" />

But when I'll send the form at 'insertmultiple.php' I use the following code but only shows 2 results. Don't matter how many times I tried show the rest, don't works:

但是当我在'insertmultiple.php'发送表单时,我使用以下代码,但只显示2个结果。无所谓多少次我尝试显示其余的,不工作:

foreach($_POST['Pago'] as key => $val) {
$Producto = $_POST['Producto'][$key];
$FormaPago = $_POST['FormaPago'][$key];
$FechaConsignacion = $_POST['FechaConsignacion'][$key];
$Valor = $_POST['Valor'][$key];
$Detalle = $_POST['Detalle'][$key];
$FechaRegistrar = $_POST['FechaRegistrar'][$key];

echo $Pago."&nbsp;--&nbsp;".$FechaConsignacion."&nbsp;--&nbsp;".$Producto."&nbsp;--&nbsp;";
echo $FormaPago."&nbsp;--&nbsp;".$Valor."&nbsp;--&nbsp;".$Detalle."&nbsp;--&nbsp;";
echo $FechaRegistrar."<br>";

}

please help! :(

请帮忙! :(

2 个解决方案

#1


3  

First thing: not key but $key

第一件事:不是钥匙而是钥匙

foreach($_POST['Pago'] as $key => $val) {

I dont see any SQL code though.

我没有看到任何SQL代码。

ADDENDUM

附录

Hm... it seems like a really messy way to insert things into database. As Cyclone noticed you do not purify your input prior to inserting the date. This is wrong but its a story for another post :p

嗯...把东西插入数据库似乎是一种非常混乱的方式。正如Cyclone注意到,在插入日期之前,您不会净化输入。这是错的,但它是另一篇文章的故事:p

Besides consider one thing:

除了考虑一件事:

Are you sure you want to use

你确定要使用吗?

$Producto = $_POST['Producto'][$key];

and not:

并不是:

$Producto = $_POST['Producto'][$val];

You seem to store some id in those checkboxes... otherwise you may have a scenario like:

您似乎在这些复选框中存储了一些ID ...否则您可能会遇到以下情况:

1  [x]
2  [ ]
3  [ ]
4  [x]

So I blieve that $_POST['Pago'] will have only 2 fields 1 and 4... but their key will be 1 and 2 as inactive checkboxes will not have value and will not end-up in the array. I may be wrong though... shame to admit I do not remember how it works precisely O.o

所以我认为$ _POST ['Pago']只有2个字段1和4 ...但是它们的键将是1和2,因为非活动复选框没有值,并且不会在数组中结束。我可能错了......但是承认我不记得它是如何正常工作的

#2


0  

The problem is solved, thanks for your help!!!!!, that's work with the next changes:

问题解决了,感谢您的帮助!!!!!,这与下一个更改有关:

if(!empty($_POST['Pago'])) {
    $aLista = array_keys($_POST['Pago']);
    $Valor = $_POST['Valor'];
    $Pago = $_POST['Pago'];
    $Producto = $_POST['Producto'];
    $FormaPago = $_POST['FormaPago'];
    $FechaConsignacion = $_POST['FechaConsignacion'];
    $Detalle = $_POST['Detalle'];
    $FechaRegistrar = $_POST['FechaRegistrar'];
foreach($aLista as $key => $val) {
      print "$aLista[$key] => $val";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$Valor[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$Producto[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$FormaPago[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$FechaConsignacion[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$Detalle[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$FechaRegistrar[$val]";
      echo "<br><hr>";
}
unset($val);

}

}

I guess, now I can Insert that rows without problems,

我猜,现在我可以毫无问题地插入那些行,

#1


3  

First thing: not key but $key

第一件事:不是钥匙而是钥匙

foreach($_POST['Pago'] as $key => $val) {

I dont see any SQL code though.

我没有看到任何SQL代码。

ADDENDUM

附录

Hm... it seems like a really messy way to insert things into database. As Cyclone noticed you do not purify your input prior to inserting the date. This is wrong but its a story for another post :p

嗯...把东西插入数据库似乎是一种非常混乱的方式。正如Cyclone注意到,在插入日期之前,您不会净化输入。这是错的,但它是另一篇文章的故事:p

Besides consider one thing:

除了考虑一件事:

Are you sure you want to use

你确定要使用吗?

$Producto = $_POST['Producto'][$key];

and not:

并不是:

$Producto = $_POST['Producto'][$val];

You seem to store some id in those checkboxes... otherwise you may have a scenario like:

您似乎在这些复选框中存储了一些ID ...否则您可能会遇到以下情况:

1  [x]
2  [ ]
3  [ ]
4  [x]

So I blieve that $_POST['Pago'] will have only 2 fields 1 and 4... but their key will be 1 and 2 as inactive checkboxes will not have value and will not end-up in the array. I may be wrong though... shame to admit I do not remember how it works precisely O.o

所以我认为$ _POST ['Pago']只有2个字段1和4 ...但是它们的键将是1和2,因为非活动复选框没有值,并且不会在数组中结束。我可能错了......但是承认我不记得它是如何正常工作的

#2


0  

The problem is solved, thanks for your help!!!!!, that's work with the next changes:

问题解决了,感谢您的帮助!!!!!,这与下一个更改有关:

if(!empty($_POST['Pago'])) {
    $aLista = array_keys($_POST['Pago']);
    $Valor = $_POST['Valor'];
    $Pago = $_POST['Pago'];
    $Producto = $_POST['Producto'];
    $FormaPago = $_POST['FormaPago'];
    $FechaConsignacion = $_POST['FechaConsignacion'];
    $Detalle = $_POST['Detalle'];
    $FechaRegistrar = $_POST['FechaRegistrar'];
foreach($aLista as $key => $val) {
      print "$aLista[$key] => $val";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$Valor[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$Producto[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$FormaPago[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$FechaConsignacion[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$Detalle[$val]";
      echo "&nbsp;&nbsp;------&nbsp;&nbsp;";
      print "$FechaRegistrar[$val]";
      echo "<br><hr>";
}
unset($val);

}

}

I guess, now I can Insert that rows without problems,

我猜,现在我可以毫无问题地插入那些行,