【问题标题】:Set update form checkbox array to 0 instead of NULL将更新表单复选框数组设置为 0 而不是 NULL
【发布时间】:2012-05-31 02:06:10
【问题描述】:

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

【问题讨论】:

    标签: php mysql arrays checkbox


    【解决方案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] =>) 
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 使用唯一名称将不起作用,因为行数总是在变化。
      • 假设您使用 php 生成表单(您就是这样),在复选框名称中包含某种计数器或实际 id 是微不足道的。就像我说的,你也可以使用单选按钮而不是复选框。
      • 我明白你现在的意思了。我发布了一个答案,但我给你一张支票,让我直截了当!谢谢。
      猜你喜欢
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 2018-05-13
      • 2017-08-31
      • 1970-01-01
      相关资源
      最近更新 更多