473,790 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_arr ay($result)) {
echo '<tr>' .
'<td>' . $displayletter['letter_id'] . '</td>' .
'<td><a href=display_le tter.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"><inpu t 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.gi f" 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<$co unt;$i++){
$updatequery = "UPDATE Letters SET letter_gallery= '$publish[$i]'
WHERE letter_id='$upd ate_id[$i]'";

if ( !$result1 = mysql_query($up datequery) ) {
echo $updatequery;
die("<p class=alert>Cou ld not save the record!<br />ID: $id</
p>");

} else {
echo implode($publis h);
echo $updatequery;
// echo "<meta http-equiv=\"refresh \" content=
\"0;URL=publish _letter.php\">" ;
echo "<p class=alert>Rec ords updated. </p>";

}

}}}

Any help you can provide is MUCH appreciated!!

May 31 '07 #1
1 3209
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_arr ay($result)) {
echo '<tr>' .
'<td>' . $displayletter['letter_id'] . '</td>' .
'<td><a href=display_le tter.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"><inpu t 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.gi f" 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<$co unt;$i++){
$updatequery = "UPDATE Letters SET letter_gallery= '$publish[$i]'
WHERE letter_id='$upd ate_id[$i]'";

if ( !$result1 = mysql_query($up datequery) ) {
echo $updatequery;
die("<p class=alert>Cou ld not save the record!<br />ID: $id</
p>");

} else {
echo implode($publis h);
echo $updatequery;
// echo "<meta http-equiv=\"refresh \" content=
\"0;URL=publish _letter.php\">" ;
echo "<p class=alert>Rec ords 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*******@attgl obal.net
=============== ===
May 31 '07 #2

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

Similar topics

2
3966
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 checkboxes are picked up using $_POST and put into session variables. On page two there is another form which is simply a condensed version of the previous one (titles with no descriptions). The checkboxes are named the same on both forms. When...
4
2982
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 an additional 2 checkboxes and presses Update button. Item count now shows 5 checkboxes are checked and "New" checkboxes remain checked. 3. Page 2:User is happy and submits form to page 3 4. Page 3:Form items are converted into printable...
0
3740
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 database? Basically, I have a form on which I have a series of statements, with 3 checkboxes for each statement - YES, NO, MAYBE. This series of statements is being dynamically populated from a query of a table in Access, called "tblQuestions". In...
7
1573
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 handles Contacts and Events. There's 1820 Contacts and 71 Events so far. The core of the DB is 3 tables viz Contacts, Events and one called Attendance that links the other two. There are several continuous forms with check boxes. For example the...
10
3300
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 multiple genres to a singe artist. Now i've searched google, but can't find a solution on how to update rows with checkboxes. If an artist gets his genre updated as follows:
5
3002
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 Thanks for help.
8
7998
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 appears saying "You are about to update xxx records. .... Are you sure you want to update these?" to which I click yes. However, when I check the table, none of the checkboxes have been updated - they are all still empty.
3
5157
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" type="checkbox" class="checkbox"'); if ($row == 1) { echo (' value="'.$row.'" checked />
8
2477
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 error. Any ideas?
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.