2

我有一个从数据库动态填充的更新表单,我的表单中有两个复选框可以发布到一个数组,因此我可以点击一个提交按钮并使用 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();
?>
4

2 回答 2

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

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

例如

假设您有 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] =>) 

另请参阅 复选框“已检查”-当数组为空时,值会跳转到较早的数组值

于 2012-05-30T18:06:10.220 回答
0

浏览器不会提交未选中的复选框。您需要提供唯一的名称,或者只需使用单选按钮。

于 2012-05-30T17:01:13.417 回答