2012-08-07 66 views
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"; 
} 
?> 
+0

你應該真的使用CSS的顏色和佈局 – Don 2012-08-07 16:03:12

+0

沒錯,這只是爲了表明我希望它看起來粗糙。我不知道怎麼說。 – Alexandra 2012-08-07 16:05:30

回答

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

也一如既往值得注意的mysql_*功能已被棄用,你應該考慮開始使用任何mysqli_*PDO方法。

+0

是的,我聽到了,非常感謝你!我確實有這樣的想法,但認爲如果我把它拿出來,它不會作出差異,但不知道它不會起作用。再次感謝! – Alexandra 2012-08-07 19:21:01

+0

@Alexandra沒問題!很高興起作用=] – newfurniturey 2012-08-07 19:24:15