Posted by hmlarson on 05/31/07 20:44
I have a form/table with checkboxes that I would like the user to
check / uncheck if they want a certain record to display in a gallery
on a website. I'm having problems figuring out how to construct the
update query so that it picks up when a user checks/unchecks the
checkbox and updates the db w/ the matching record ID#.
Right now, if a user unchecks a box, the value does not get passed
and the DB does not update - set/change it to 0. The only values that
get passed are the checkboxes that are checked and these end up being
mismatched with the matching id (passed in a hidden field).
The checkboxes look like this:
<?
while ($displayletter = mysql_fetch_array($result)) {
echo '<tr>' .
'<td>' . $displayletter['letter_id'] . '</td>' .
'<td><a href=display_letter.php?letter_id=' .
$displayletter['letter_id'] . ' target=blank> ' .
$displayletter['letter_title'] . '</td>' .
'<td>' . $displayletter['submit_date'] . '</td>' .
'<td>' . $displayletter['response_date'] . '</td>';
if ($displayletter['letter_gallery'] == "1") {
echo '<td align="center"><input type=checkbox name="publish[]"
value="1" CHECKED><input type=hidden name="update_id[]" value="' .
$displayletter['letter_id'] . '" ></td>' ;} i
f ($displayletter['letter_gallery'] == "0") {
echo '<td align="center"><input type=checkbox UNCHECKED
name="publish[]" value="0"><input type=hidden name="update_id[]"
value="' . $displayletter['letter_id'] . '" ></td>' ; }
echo '</tr>';
}
} else { //display error message if something wrong
echo '<p>' . mysql_error() . '<p>';
}
?>
</table>
<p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="image" src="../images/80x30_submit.gif" name="image"
width="80" height="30" />
</p>
</form>
and my update query....
$update_id = $_POST['update_id'];
$publish = $_POST['publish'];
if (isset($_POST['submitted'])) {
if (isset($_POST['update_id'])) {
for($i=0;$i<$count;$i++){
$updatequery = "UPDATE Letters SET letter_gallery='$publish[$i]'
WHERE letter_id='$update_id[$i]'";
if ( !$result1 = mysql_query($updatequery) ) {
echo $updatequery;
die("<p class=alert>Could not save the record!<br />ID: $id</
p>");
} else {
echo implode($publish);
echo $updatequery;
// echo "<meta http-equiv=\"refresh\" content=
\"0;URL=publish_letter.php\">";
echo "<p class=alert>Records updated. </p>";
}
}}}
Any help you can provide is MUCH appreciated!!
Navigation:
[Reply to this message]
|