我有一个动态填充数据库的更新表单,我的表单中有两个复选框,发布到数组,因此我可以点击一个提交按钮并使用foreach语句更新所有行。文本字段可以正常工作,但是当复选框字段发布到其数组时,它们会忽略我的数组变短的零。
如何在null复选框的位置添加0?
这是我的表格
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<input type="hidden" name="ss_id[]" value="<?php echo $row_rsSnapshot['ss_id']; ?>" />
<input type="hidden" name="ss_yearmonth[]" value="<?php echo htmlentities($row_rsSnapshot['ss_yearmonth'], ENT_COMPAT, 'UTF-8'); ?>" />
<tr>
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_inventory'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_inventory[]" value="" <?php if (!(strcmp(htmlentities($row_rsSnapshot['ss_inventory'], ENT_COMPAT, 'UTF-8'),""))) {echo "checked=\"checked\"";} ?> />
</td>
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_write_off'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_write_off[]" value="" <?php if (!(strcmp(htmlentities($row_rsSnapshot['ss_write_off'], ENT_COMPAT, 'UTF-8'),""))) {echo "checked=\"checked\"";} ?>
</td>
<td>
<input type="text" name="ss_date[]" value="<?php echo htmlentities($row_rsSnapshot['ss_date'], ENT_COMPAT, 'UTF-8'); ?>" size="32" />
</td>
<td>
<input type="text" name="ss_transaction[]" value="<?php echo htmlentities($row_rsSnapshot['ss_transaction'], ENT_COMPAT, 'UTF-8'); ?>" size="32" />
</td>...
这是我的sql更新查询
foreach($_POST['ss_id'] as $key=>$ss_id){
$ss_inventory = GetSQLValueString(isset($_POST['ss_inventory'][$key]) ? "true" : "", "defined","1","0");
$ss_write_off = GetSQLValueString(isset($_POST['ss_write_off'][$key]) ? "true" : "", "defined","1","0");
$ss_date = $_POST['ss_date'][$key];
$ss_transaction = $_POST['ss_transaction'][$key];
$ss_debit = $_POST['ss_debit'][$key];
$ss_credit = $_POST['ss_credit'][$key];
$ss_yearmonth = $_POST['ss_yearmonth'][$key];
$sql = "UPDATE snapshot SET ss_inventory = '$ss_inventory', ss_write_off = '$ss_write_off', ss_date = '$ss_date', ss_transaction = '$ss_transaction', ss_debit = '$ss_debit', ss_credit = '$ss_credit' , ss_yearmonth = '$ss_yearmonth' WHERE ss_id = '$ss_id' ";
mysql_select_db($database_connMyayla, $connMyayla);
$Result1 = mysql_query($sql, $connMyayla) or die(mysql_error());
}
}
mysql_close();
?>
答案 0 :(得分:1)
这解决了它
如果有人有兴趣......
我放了一个计数器,找出这个查询填充了多少行,并将其插入我的复选框名称数组......
$i = 0;
do {
...
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_inventory'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_inventory[<?php echo $i;?>]" value="" <?php if (in_array($i, $ss_inventory)) echo "checked='checked'"; ?> />
</td>
<td>
<input <?php if (!(strcmp($row_rsSnapshot['ss_write_off'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="ss_write_off[<?php echo $i;?>]" value="" <?php if (in_array($i, $ss_write_off)) echo "checked='checked'"; ?> />
</td>
....
$i++;
} while ($row_rsSnapshot = mysql_fetch_assoc($rsSnapshot));
现在选中的复选框将与正确的数组编号对应。
e.g。
假设您有4行数据,但只检查第2行和第4行的复选框。
//run this to see your result
print_r($_POST['ss_inventory']);
//outputs: Array ([1] => [3] =>)
在此输出之前,所有内容都被推送到我的数组的开头,因为没有提交false复选框或NULL。因此第1行和第2行将成为现实。
//outputs: Array ([0] => [1] =>)
另见 Checkbox "checked"-value jumps up to earlier array values, when array is empty
答案 1 :(得分:0)
浏览器不提交未选中的复选框。您需要提供唯一的名称,或者只需使用单选按钮。