0

我希望这是一件小事。在梳理了几个小时并尝试了各种变化之后,我决定让专业人士对此有所了解。我有一个页面设置来轻松管理数据库。基本上只是为了更新一行或删除它。对于每一行,我都有一个复选框来更新行,复选框来删除,三列来自数据库的信息,然后两列带有下拉框来更改值。问题是无论我从下拉列表中选择什么值,它总是发布相同的值。使用 print_r($_POST) 我可以看到它实际上返回了错误的值。废话不多说,代码:

这是我页面的顶部(表单发布到自身):

    if(isset($_POST['checkbox'])){
    $product = $_POST['product_choice'];
    $status = $_POST['status_choice'];
    $checkbox = $_POST['checkbox'];
    if(isset($_POST['update'])){
    $update = $_POST["update"];
    }
    $id = "('" . implode( "','", $checkbox ) . "');" ;
    $sql="UPDATE test_orders SET product='$product',status = '$status' WHERE ordNum IN $id" ;
    $result = mysql_query($sql) or die(mysql_error());
      }

和形式:

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="frmactive" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1">

<!--BUTTONS--> 

<tr>  
<td colspan="6"><input name="update" type="submit" id="update" value="Upate" />
<input name="delete" type="submit" id="delete" value="Delete" /></td>
</tr>


<tr>
<td>&nbsp;</td>
<td colspan="5"><strong>Update multiple rows in mysql with checkbox</strong> </td>
</tr>
<tr>
<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Update?</td>

<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Delete?</td>

<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Order #</td>

<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Name</td>

<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Job Name</td>

<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Product</td>

<td style="background: #ddd; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc;">Status</td>


</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>


<tr>
<td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['ordNum']; ?>"></td>
<td bgcolor="#FF0000" align="center"><input type="checkbox" name="delete[]" id="delete[]" value="<? echo $rows['ordNum']; ?>" /></td>
<td><? echo $rows['ordNum']; ?></td>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['OrderDesc']; ?></td>
<td>
<select name="product_choice" id="product" style="font-size:10px;">

            <option value="<? echo $rows['product']; ?>" selected="selected"><? echo $rows['product']; ?>
            </option>
            <option value="Color Correction"> Color Correction </option>
            <option value="Basic retouch"> Basic retouch </option>
            <option value="Extensive Retouch"> Extensive Retouch </option>
            <option value="Signature Work"> Signature Work </option>
            <option value="Signature Oil"> Signature Oil </option>
            <option value="Web Design"> Web Design </option>
            <option value="Print Design"> Print Design </option>
            <option value="Other"> Other </option>
          </select>

</td>

<td>
   <select name="status_choice" id="status" style="font-size:10px;">

            <option value="<? echo $rows['status']; ?>" selected="selected"><? echo $rows['status']; ?></option>
            <option value="Order Received"> Order Received </option>
            <option value="in the works"> in the works </option>
            <option value="waiting to upload"> waiting to upload </option>
            <option value="uploading"> uploading </option>
            <option value="ON HOLD"> ON HOLD </option>
            <option value="awaiting decision"> awaiting decision </option>
            <option value="proof sent"> proof sent </option>
            <option value="next invoice"> next invoice </option>
            <option value="waiting images/order"> waiting images/order </option>
            <option value="invoiced"> invoiced </option>
            <option value="complete"> complete </option>
            <option value="paid"> paid </option>
            <option value="making changes"> making changes </option>
          </select>

    </td>

    </tr>



<?php
}
?>
<tr>
<td colspan="6" align="center">&nbsp;</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

这是 print_r($_POST); 的结果 无论我在下拉菜单中选择什么选项。[product_choice] 和 [status_choice] 始终相同。

Array ( [update] => Upate [checkbox] => Array ( [0] => 113 ) [product_choice] => Extensive Retouch [status_choice] => Order Received )

谢谢你的帮助。

** * *已解决* ** * ** * ** 得到朋友的帮助,工作良好如下

4

2 回答 2

1

问题是您在循环中有一个带有静态名称的选择

<select name="product_choice" id="product" style="font-size:10px;">

所以你会有一堆同名的选择框,所以它们会互相覆盖,只有页面上的最后一个是正确的。您可以给它们一个唯一的名称或使其成为一个数组。

这与您的状态一相同

<select name="status_choice" id="status" style="font-size:10px;">

所以做类似的事情

<select name="status_choice[<?=$rows['id']?>]>

然后得到结果..

foreach($_POST['product_choice'] as $key => $product)
{
    $status = $_POST['status_choice'][$key];
    $sql = sprintf('UPDATE test_orders SET product = "%s", status="%s" WHERE ordNum = %s', 
        mysql_real_escape_string($product), mysql_real_escape_string($_POST['status_choice'][$key]), mysql_real_escape_string($_POST['checkbox'][$key]));
    $result = mysql_query($sql) or die(mysql_error());     
}

然后将您的复选框名称更改为checkbox[<?=$rows['product']?>]

于 2012-08-25T03:22:32.260 回答
-1

打开 foreach 循环:

foreach($_POST as $key => $value) {
    if(!is_array($value)) {
        $_POST[$key] = mysql_real_escape_string($value);
    } else {
        foreach($value as $key => $value) {
            $_POST[$key][$key] = mysql_real_escape_string($value);
        }
    }
}

//print_r($_POST);

// * Delete multiple via checkboxes * START *
if(isset($_POST['delete'])) { 
    if(is_array($_POST['delete'])) {
        foreach($_POST['delete'] as $value) { 
            if(intval($value)) {
                echo 'The order number '.$value.' has been deleted';
                $sql_query = mysql_query("DELETE FROM $tbl_name WHERE ordNum = $value"); 
            }
        } 
    }
} elseif(isset($_POST['checkbox'])) {
    if(isset($_POST['product_choice'])) {
        foreach($_POST['product_choice'] as $key => $value) {
            if(intval($key)) {
                $sql_query = 'UPDATE '.$tbl_name.' SET product = "'.$_POST['product_choice'][$key].'", 
                              status = "'.$_POST['status_choice'][$key].'" WHERE ordNum = '.$key;
                echo $result = mysql_query($sql_query) or die(mysql_error()); 
            }
        }
    }
}

和下拉框:

<td>
<?//php echo $rows['product']; ?>
<select name="product_choice[<?php echo $rows['ordNum']?>] id="product" style="font-size:10px;">
            <option value="<?php echo $rows['product']; ?>" selected="selected"><?php echo $rows['product']; ?></option>
            <option value="Color Correction"> Color Correction </option>
            <option value="Basic retouch"> Basic retouch </option>
            <option value="Extensive Retouch"> Extensive Retouch </option>
            <option value="Signature Work"> Signature Work </option>
            <option value="Signature Oil"> Signature Oil </option>
            <option value="Web Design"> Web Design </option>
            <option value="Print Design"> Print Design </option>
            <option value="Other"> Other </option>
          </select>

</td>

<td>
   <select name="status_choice[<?php echo $rows['ordNum']?>] id="status" style="font-size:10px;">

            <option value="<?php echo $rows['status']; ?>" selected="selected"><?php echo $rows['status']; ?></option>
            <option value="Order Received"> Order Received </option>
            <option value="in the works"> in the works </option>
            <option value="waiting to upload"> waiting to upload </option>
            <option value="uploading"> uploading </option>
            <option value="ON HOLD"> ON HOLD </option>
            <option value="awaiting decision"> awaiting decision </option>
            <option value="proof sent"> proof sent </option>
            <option value="next invoice"> next invoice </option>
            <option value="waiting images/order"> waiting images/order </option>
            <option value="invoiced"> invoiced </option>
            <option value="complete"> complete </option>
            <option value="paid"> paid </option>
            <option value="making changes"> making changes </option>
          </select>

    </td>

于 2012-08-28T19:56:22.483 回答