字段未在数组中更新

时间:2016-04-24 06:15:35

标签: php mysql arrays forms

尝试使用所选框的复选框更新mysql dbase。它会更新表,但行是空的而不是字段的值。

我的代码

<table width="98%" border="0" align="center" cellpadding="3" cellspacing="0" class="big_table2">
      <tr>
        <td colspan="12" bgcolor="#FF0000" class="16_white">Awaiting Confirmation</td>
      </tr>
      <tr class="form_name">
        <td>&nbsp;</td>
        <td>SN</td>
        <td>Date</td>
        <td>Office</td>
        <td>Prod</td>
        <td>Amount</td>
        <td>Amt Naira</td>
        <td>Hash</td>
        <td>Batch</td>
        <td>comment</td>
        <td>status</td>
      </tr>
            <form id="pur" name"pur" action="savetodb.php?lrpur_sn=<?php echo $row_lrpur['sn']; ?>" method="POST" >
      <?php do { ?>
      <tr class="body_text_fade">
        <td><input type="checkbox" name="sn[]" value="<?php echo $row_lrpur['sn']; ?>" /></td>
        <td><?php echo $row_lrpur['sn']; ?></td>
        <td><?php echo $row_lrpur['date']; ?></td>
        <td><?php echo $row_lrpur['office']; ?></td>
        <td><?php echo $row_lrpur['product']; ?></td>
        <td>$<?php echo number_format($row_lrpur['amount'],4); ?></td>
        <td>N<?php echo number_format($row_lrpur['amtnaira'],2); ?></td>
                    <? if($row_lrpur['product'] =='Bit Coin') { ?>
        <td><a href="https://blockchain.info/tx/<?php echo $row_lrpur['batch']; ?>" target="_blank">Hash</a></td>
        <? }else{ ?>
        <td><?php echo $row_lrpur['batch']; ?></td>
        <? } ?>

        <td><?php echo $row_lrpur['comment']; ?></td>
        <td><textarea name="comment2" cols="15" id="comment2" style="color: #66CCFF; border: 1px double #66CCFF; background-color: #FFFFFF"></textarea></td>
        <td><select name="status2" id="status2">
          <option value="confirmed" selected>V</option>
          <option value="query">Q</option>
        </select></td>

      </tr>
      <?php if ($row_lrpur['AdminComment'] != '') { ?>
      <tr class="body_text_fade">
        <td bgcolor="#000000">&nbsp;</td>
        <td bgcolor="#000000">&nbsp;</td>
        <td bgcolor="#000000"><span class="body_text_white_nopadding_top">Query</span></td>
        <td colspan="9" class="body_text_red"><?php echo $row_lrpur['AdminComment']; ?></td>
      </tr>
                <?php }
      } while ($row_lrpur = mysql_fetch_assoc($lrpur)); ?>
      <tr class="body_text_fade">
        <td height="0">&nbsp;</td>
        <td>&nbsp;</td>
        <td height="0">&nbsp;</td>
        <td height="0" colspan="2">Enter Password:</td>
        <td height="0" colspan="2" class="body_text_red"><input name="pass" type="password" id="pass" size="10" style="color: #66CCFF; border: 1px double #66CCFF; background-color: #FFFFFF" /></td>
        <td height="0" colspan="3" class="body_text_red"><input type="submit" name="updatepurchases" id="updatepurchases" value="Confirm Checked"></td>
        <td class="body_text_red"><input type="reset" name="resetchecked" id="resetchecked" value="Reset Checked" /></td>
        <td class="body_text_red">&nbsp;</td>

        </tr>
            </form>
  </table>

这是php代码:

if ($_POST['pass'] == 'hfdhy67w' && $_GET['lrpur_sn'] !=" ") { 
//$sn = $_POST['sn'];

//$impid = implode(", ",$sn);


foreach($_POST["sn"] as $sn) {

$comment2 = mysql_real_escape_string($_POST["comment2"][$sn]);
$status2 = mysql_real_escape_string($_POST["status2"][$sn]);

$sql = mysql_query("UPDATE LRPurchases SET status= '$status2', AdminComment= '$comment2' WHERE sn= $sn")
or die(mysql_error());
if(isset($sql)) {
echo " <h4> Confirmed </h4";
}}
} else {
    echo 'Wrong Password'.'</br>';
}

$ status2和$ comment2始终更新为空。如果我删除[$ sn],它将在所有字段中更新相同的值而不是更新的值。

1 个答案:

答案 0 :(得分:0)

像这样使用:

    <td><input type="checkbox" name="sn[]" value="<?php echo $row_lrpur['sn']; ?>" /></td>
    <td><textarea name="comment2[]" cols="15" style="color: #66CCFF; border: 1px double #66CCFF; background-color: #FFFFFF"></textarea></td>
    <td><select name="status2[]">

在PHP中:

if ($_POST['pass'] == 'hfdhy67w' && $_GET['lrpur_sn'] !=" ") { 
$sn = $_POST['sn'];
$comment2 = $_POST['comment2'];
$status2 = $_POST['status2'];

for($i=0;$i<count($sn);$i++){

       $comment2 = mysql_real_escape_string($comment2[$i]);
       $status2 = mysql_real_escape_string($status2[$i]);
       $sn_value = $sn[$i];

       $sql = mysql_query("UPDATE LRPurchases SET status= '$status2',  AdminComment= '$comment2' WHERE sn= $sn_value")
       or die(mysql_error());
    if(isset($sql)) {
         echo " <h4> Confirmed </h4";
    } else {
       echo 'Wrong Password'.'</br>';
    }
}