473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

checkboxes and update query

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

May 31 '07 #1
1 3187
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.
js*******@attglobal.net
==================
May 31 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
4
by: Pete | last post by:
Okay, I'm still stuck with this problem. Here's a quick recap/summary :- 1. Page 1:User checks 3 out of 10 checkboxes and submits form to page 2 2. Page 2:Item count shows 3 items. User checks...
0
by: Frank Collins | last post by:
Can anyone point me to some good examples on the web of using values from dynamically created checkboxes on forms in ASP, particularly relating to INSERTING those values into a SQL or Access...
7
by: Chantelle | last post by:
I've been delighted with the responses I've had from all you guys in relation to the two queries I raised recently and I'm back with another challenge! Here goes. I've got this DB in A2K that...
10
by: frizzle | last post by:
Hi there, I'm building a music site with a mysql backend. It has a many to many relational database. I use this to match music genres with certain artists, to maintain the possibility to add...
5
by: colleen1980 | last post by:
Hi: In my table there is a field of type checkbox. I create a button on my form and wants to deselect all the checkboxes in that field (PrintQueue). Table: Research_New PrintQueue Format Yes/No...
8
by: rch2 | last post by:
Hi, I'm trying to use an update query to update a checkbox field in access 97. In the update query design I am indicating in criteria: =true When I run the update query, a message box...
3
by: mountain.dog | last post by:
I have a query that shows a list of options that a user can toggle on or off using a checkbox. query... form... while($row = mysql_fetch_array($result))... <input name="menu_show_attribute"...
8
by: HowardChr | last post by:
Is there a way to code a command button on a form that will clear all "-1" Yes/No fields on a table to show as "0"? I tried to type in: "update "InputH- 1" Set Chooser = 0", but am getting an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.