Question

I figured out how to update a table with multiple columns using checkboxes, except when more than one checkbox is selected the update will only happen for one of the columns not both. Could someone please help? Thank you!

Here is the working code for the table:

<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>

And this is what I have for the php page that take the info.

// 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";
}
?>
Was it helpful?

Solution

In your HTML, you're defining your checkboxes with the name checkbox - so only one value will be sent to PHP. To get an array of values, update the checkboxes to be created with name="checkbox[]" and when the form is submitted, you can use the selected values like an array.

In that case, you'll be able to loop through each item with:

$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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top