php - 数组中的字段未更新

标签 php mysql arrays forms

我正在尝试使用所选框的复选框来更新 mysql 数据库。它更新表,但行为空,而不是字段的值。

我的代码

<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],它将更新所有字段中的相同值而不是不同的值。

最佳答案

像这样使用:

    <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>';
    }
}

关于php - 数组中的字段未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36819923/

相关文章:

java - 将字符串分解为 int 数组

c - C中解析指针参数

php - 带有原型(prototype)的 ajax 请求返回 200 成功并带有空白 html 页面(间歇性)

php - 如何运行批处理?

php - 搜索和分页

c# - 如何从表中选择所有 SQL 行(具有两列的唯一组合),其中相同的组合不在另一个表中

java - 不知道是什么导致了我的 ArrayIndexOutOfBoundsException 错误

PHP http_build_url() 和 PECL 安装

php - 如何使用 PayPal rest API 添加通用 "extra fee"?

php - 访问模型父对象中的对象