簡體   English   中英

我無法更新多列,只有一列會更新

[英]I cannot update multiple columns, only one will update

我想出了如何使用復選框更新具有多列的表的方法,除了選擇多個復選框時,更新只會針對其中一列而不是兩列進行。 有人可以幫忙嗎? 謝謝!

這是該表的工作代碼:

<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";
}
?>

在您的HTML中,您正在使用名稱checkbox定義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);
}

而且,一如既往,值得注意的是,不建議使用mysql_*函數,您應該考慮開始使用mysqli_*PDO方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM