Re: checkboxes and update query
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Thu, 31 May 2007 16:58:59 -0400
hmlarson wrote:
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!!
Yes, the mismatch is a problem with checkboxes - which is why you can't use just update_id[] with them.
What you need to do is give each checkbox a unique id - maybe something like update_id[$display_letter['letter_id']. Then have a hidden field in each one such as
<input type=hidden name="recid[]" value="$display_letter['letter_id']>
The hidden field will allow you to track each record displayed (I'm assuming $display_letter['letter_id'] is a unique id). Then you can check to see if your checkbox is checked by testing the appropriate update_id array element.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- checkboxes and update query
- From: hmlarson
- checkboxes and update query
- Prev by Date: checkboxes and update query
- Previous by thread: checkboxes and update query
- Index(es):
Relevant Pages
|