php update multiple rows based on checkbox selections
Posted: Fri Jun 24, 2011 9:00 pm
hi everyone:
i need to update multiple rows based on if checkboxes are selected. i have a dropdon list with case workers names, when a name is selected from the list, and the input button 'apply' is clicked, i need to update every row with the new case workers name. i've been struggling with it for quite a while so i'm posting to see if anyone could have any pointers for me?
every row has a checkbox and when any checkbox is selected, i need to update the caeworkers name to the name selected from the caseworker dropdown list. currently nothing happens when 'apply' is clicked. i'm not sure what i'm doing wrong.
my code is:
i need to update multiple rows based on if checkboxes are selected. i have a dropdon list with case workers names, when a name is selected from the list, and the input button 'apply' is clicked, i need to update every row with the new case workers name. i've been struggling with it for quite a while so i'm posting to see if anyone could have any pointers for me?
every row has a checkbox and when any checkbox is selected, i need to update the caeworkers name to the name selected from the caseworker dropdown list. currently nothing happens when 'apply' is clicked. i'm not sure what i'm doing wrong.
my code is:
Code: Select all
<?php
if(isset($_POST['Apply'])){
$NewCaseWorker = $_POST['CaseWorker'];
foreach($_POST['selectreassign'] as $key => $value) {
mysql_query("UPDATE records SET CaseWorker='$NewCaseWorker' WHERE id='$value')") or die(mysql_error());
}
}
?>
<form id="form1" name="form1" method="post" action="">
<table width="300" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>Reassign To</td>
<td>
<?php
$result=mysql_query("SELECT ProperName FROM caseworker");
$options="";
while ($row=mysql_fetch_array($result)) {
$selected = ($row['ProperName']==$CaseWorker) ? ' selected="selected"' : '';
$options .= "<option value=\"{$row['ProperName']}\"{$selected}>{$row['ProperName']}</option>\n";
}
?>
<select name="CaseWorker" id="CaseWorker" >
<option value="">< select owner > <?php echo $options ?></option>
</select>
<input type="submit" name="Apply" id="Apply" value="Apply" />
</td>
</tr>
</table>
</form>
<br />
<?php
$ReassignTo = $_POST['CaseWorker'];
// get results from database
$string = "SELECT * FROM records ORDER BY id DESC";
$query = mysql_query($string) or die (mysql_error());
$num_rows = mysql_num_rows($query);
if($num_rows>0) {
$pages = new Paginator;
$pages->items_total = $num_rows;
$pages->paginate();
echo $pages->display_pages();
echo $pages->display_items_per_page();
echo '<hr>';
} else {
echo "<div id='titles_smaller'>No data available.</div><br />";
}
echo "<table class='sortable' width='100%' border='0' cellpadding='1'>";
echo "<tr><th nowrap>Select</th> <th nowrap>ID #</th><th nowrap>Company Name</th><th nowrap>Case Worker</th><th nowrap>Recorded</th></tr>";
$string = $string."$pages->limit";
$query = mysql_query($string) or die (mysql_error());
$result = mysql_fetch_array($query);
if($result==true) {
do
{
$id = $result['id'];
echo "<tr>";
echo "<td>";
echo "<input type='checkbox' name='selectreassign[]' value='<?php echo $id ?>' />";
echo "</td>";
echo '<td nowrap><a href="details.php?id=' . $result['id'] . '">' . $result['id'] . '</a></td>';
echo '<td nowrap>' . $result['CompanyName'] . '</a></td>';
echo '<td nowrap>' . $result['CaseWorker'] . '</td>';
echo '<td nowrap>' . $result['DateRecorded'] . '</td>';
echo "</tr>";
}
while($result = mysql_fetch_array($query));
}
// close table>
echo "</table><hr>";
if($num_rows>0) {
echo $pages->display_pages().$pages->display_items_per_page();
}
// end pagination
}
?>