所以我正在查看本网站上的其他帖子,并且遇到了下面的代码(我的代码的编辑),除了我提交时我的错误代码完全有效:注意:未定义的索引:id在第17行的/customers/0/2/e/richardbrown.name/httpd.www/debt/payment_process.php 中为每个结果。
<form method="post" action="payment_process.php">
<table border="0">';
$stmt = $db->query("SELECT * FROM debt_accounts LEFT JOIN debt_companies ON accounts_company=companies_id WHERE accounts_amount > 0 ORDER BY accounts_company ASC");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo'<tr>
<td width="200" align="left"><input type="text" name="payment[][id]" value="'.$row['accounts_id'].'" /></td>
<td width="100" align="right"><input type="text" name="payment[][amount]" value="25.00" /></td>
</tr>';
}
echo'</table><br /> <br /><input type="submit" /></form>
if ( isset( $_POST['payment'] ) )
{
echo '<table>';
foreach ( $_POST['payment'] as $diam )
{
echo '<tr>';
echo ' <td>', $diam['id'], '</td>';
echo ' <td>', $diam['amount'], '</td>';
echo '</tr>';
}
echo '</table>';
}
答案 0 :(得分:2)
从表单元素更改此行:
name="payment_amount"
是一个数组:
name="payment_amount[]"
然后,将您的foreach
修改为:
foreach ($_POST['payment_amount'] as $value) {
echo $value . "<br>";
}
name="payment_id[]"
要将它们全部添加,如果您想这样做,请使用:
foreach ($_POST['payment_amount'] as $value) {
echo $value . "<br>";
$total += $value;
}
echo $total;
或
$total = array_sum($_POST['payment_amount']);
答案 1 :(得分:0)
您的解决方案是: 您表单中的name =“payment_id []”
和服务器端(payment_process.php) 你使用带有$ _POST ['payment_id'] []的foreach循环。