0

我想出了如何使用复选框更新具有多列的表,除非选择了多个复选框,更新只会发生在其中一列而不是两者。有人可以帮忙吗?谢谢!

这是该表的工作代码:

<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="frmactive" method="GET" action="assigncases1.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>

          <select name="assigned_to">
        <option value=""></option>            
        <option value="Kristin Dodd"> Kristin Dodd </option>
        <option value="Matt Ursetto"> Matt Ursetto </option>
        <option value="Derek Bird"> Derek Bird </option>
        <option value="John Castle"> John Castle </option>
        <option value="Martin Delgado"> Martin Delgado </option>
      </select>
<br>
<input type='submit' value = 'Assign'> 
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="4" align="center"><strong>Update multiple rows in mysql with checkbox<br>
</strong></td>
</tr><tr>
<td align="center" bgcolor="#FFFFFF"></td>
<td align="left"><strong>Name</strong></td>
<td align="left"><strong>SSO</strong></td>
<td align="left"><strong>case_status</strong></td>
<td align="left"><strong>Assigned To:</strong></td>
</tr>
<?php
$result=mysql_query($sql);

$count=mysql_num_rows($result);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="checkbox" type="checkbox" id="checkbox[]" value="<?        
echo $rows['id']; ?>"></td>
<td><? echo $rows['full_name']; ?></td>
<td><? echo $rows['sso']; ?></td>
<td><? echo $rows['case_status']; ?></td>
<td><? echo $rows['assigned_to']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center">&nbsp;</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

这就是我为获取信息的 php 页面所拥有的。

// Retrieve data from database 
$checkbox = $_GET['checkbox'];
$assigned_to = $_GET['assigned_to'];

// update data in mysql database 
$sql="UPDATE rmstable2 SET assigned_to='$assigned_to' WHERE id='$checkbox'";
$result = mysql_query($sql);
$count = mysql_numrows($result);
{
mysql_query("UPDATE `rmstable2` SET `assigned_to` = '$assigned_to'
                         WHERE `id` = '$checkbox'") 
 or die(mysql_error());

}

 //If they did not enter a search term we give them an error 
if ($assigned_to == "")
{ 
echo "<h3>You forgot to enter a required field. Please try again.</h3><br>";  
 } 
// if successfully updated. 
else if($assigned_to !== "")
{
echo "<b>Update Successful</b><br>";
echo "<br>This case was assigned to:<b> $assigned_to</b><br>"; 
echo "<br>To see the change go back and click on <b>Refresh Page</b> if you assigned it
to someone other than you, the case will disappear and show up in their list of      
 assigned cases."; 
}
else {
echo "ERROR";
}
?>
4

1 回答 1

0

在您的 HTML 中,您使用名称定义复选框checkbox- 因此只有一个值将发送到 PHP。要获取值数组,请更新要创建的复选框,name="checkbox[]"并且在提交表单时,您可以像数组一样使用选定的值。

在这种情况下,您将能够通过以下方式遍历每个项目:

$checkbox = $_GET['checkbox'];
$assigned_to = $_GET['assigned_to'];

foreach ($checkbox as $id) {
    // do a very basic validation to see if the ID is numeric
    if (!is_numeric($id)) continue;
    $sql = 'UPDATE rmstable2 SET assigned_to="' . mysql_real_escape_string($assigned_to) . '" WHERE id=' . (int)$id;
    mysql_query($sql);
}

Also, as always worth noting, the mysql_* functions are being deprecated and you should consider to start using either mysqli_* or PDO methods.

于 2012-08-07T19:09:01.443 回答