gpt4 book ai didi

php - 将更新表单复选框数组设置为 0 而不是 NULL

转载 作者:行者123 更新时间:2023-11-29 03:09:18 25 4
gpt4 key购买 nike

我有一个从数据库动态填充的更新表单,我的表单中有两个复选框发布到一个数组,因此我可以点击一个提交按钮并使用 foreach 语句更新所有行。文本字段按应有的方式工作,但是当复选框字段发布到它们的数组时,它们省略了零,我的数组变得更短。

如何在空复选框所在的位置添加 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();
?>

最佳答案

这个解决了

如果有人有兴趣...

我设置了一个计数器来查明有多少行正在用这个查询填充,并将其插入到我的复选框名称数组中......

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

现在选中的复选框将对应正确的数组编号..

例如

假设您有 4 行数据,但您只选中了第 2 行和第 4 行的复选框。

//run this to see your result

print_r($_POST['ss_inventory']);

//outputs: Array ([1] => [3] =>)

在这是我的输出之前,所有内容都被推到我的数组的开头,因为错误的复选框没有被提交或为 NULL。所以第 1 行和第 2 行的结果为真。

//outputs:  Array ([0] => [1] =>) 

另见 Checkbox "checked"-value jumps up to earlier array values, when array is empty

关于php - 将更新表单复选框数组设置为 0 而不是 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10820814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com