473,763 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using checkboxes to update multiple database entries

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_arr ay($result))...
<input name="menu_show _attribute[]" type="checkbox"
class="checkbox "');
if ($row['menu_show_attr ibute'] == 1) {
echo (' value="'.$row[menu_id].'" checked />
} else {
echo (' value="'.$row[menu_id].'" />');
}

The list is generated from boolean values in a DB. This populates my
form with checkboxes, some are checked and others are not. I'm using
an if statement to sort it (checked). This works fine.

The problem is when the user un-checks a checkbox, 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. Below is
the query / code after the submit button has been pressed. Are radio
buttons the answer? Any help / suggestions would be very much
appreciated.

//process form
for ($i = 0; $i < count($menu_sho w_attribute); $i++) {

if (isset($menu_id ) == 'checked') {
$menu_show_attr ibute = 1;
} elseif ($menu_show_att ribute != 'checked') {
$menu_show_attr ibute = 0;
}

$query = 'UPDATE menu SET menu_show_attri bute = "'.
$menu_show_attr ibute.'" WHERE menu_id = "'.
$menu_show_attr ibute[$i].'"';
echo("<br>");
echo $query;
$result = mysql_query($qu ery, $db) or die(mysql_error ());
}

Mar 29 '07 #1
3 5156
Message-ID: <11************ **********@b75g 2000hsg.googleg roups.comfrom
mo**********@gm ail.com contained the following:
>The problem is when the user un-checks a checkbox, 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. Below is
the query / code after the submit button has been pressed. Are radio
buttons the answer? Any help / suggestions would be very much
appreciated.
Checkboxes only return a value if checked. The solution is to make your
update query update all fields with the default value set to unchecked.
The unchecked fields will automatically be unchecked because the POSTed
value will not exist.

Of course you will have to explicitly name your check boxes something
like <input name='menu_show _attribute[".$row['menu_id']."]'
type='checkbox'
class='checkbox ')

And of course you won't be able to loop through the array
$_POST['menu_show_attr ibute'] because the unchecked values will not
show. However you could include a hidden field
<input name='hidden_me nu_id[".$row['menu_id']."]' type='hidden'>

Then update your db by looping through $_POST['hidden_menu_id ']

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 29 '07 #2
Thanks. I had a feeling checkboxes were not going to work. Oh well.
Do think radio buttons would work?

On Mar 29, 9:43 am, Geoff Berrow <blthe...@ckdog .co.ukwrote:
Message-ID: <11************ **********@b75g 2000hsg.googleg roups.comfrom
mountain....@gm ail.com contained the following:
The problem is when the user un-checks a checkbox, 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. Below is
the query / code after the submit button has been pressed. Are radio
buttons the answer? Any help / suggestions would be very much
appreciated.

Checkboxes only return a value if checked. The solution is to make your
update query update all fields with the default value set to unchecked.
The unchecked fields will automatically be unchecked because the POSTed
value will not exist.

Of course you will have to explicitly name your check boxes something
like <input name='menu_show _attribute[".$row['menu_id']."]'
type='checkbox'
class='checkbox ')

And of course you won't be able to loop through the array
$_POST['menu_show_attr ibute'] because the unchecked values will not
show. However you could include a hidden field
<input name='hidden_me nu_id[".$row['menu_id']."]' type='hidden'>

Then update your db by looping through $_POST['hidden_menu_id ']

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDshttp://www.ckdog.co.uk/rfdmaker/

Mar 29 '07 #3
Message-ID: <11************ **********@l77g 2000hsb.googleg roups.comfrom
mo**********@gm ail.com contained the following:
>Thanks. I had a feeling checkboxes were not going to work.
Where did I say that? Of course checkboxes will work.
>Do think radio buttons would work?
No.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 29 '07 #4

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

Similar topics

2
31129
by: Rima | last post by:
I have the following two tables : table a (commit_id, capital_market_id, chg_lst_date ) table b (b_seq_id,
10
3298
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:
16
3498
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown list and checkbox so that they can make changes. I named these elements as arrays and wish to run an...
6
4354
by: Daz | last post by:
Hi everyone. Firstly, I apologise if this i not what you would call a PHP problem. I get quite confused as to what lives in which realm, so if this shouldn't be posted here, please suggest where I should post it. I have created a form, which consists of a list of items, each with a checkbox. When a checkbox is checked or unchecked, the page should be refreshed. During the refresh, the data is validated and the MySQL database is...
10
2240
by: chimambo | last post by:
Hi All, I have a little problem. I am retrieving records from a table and I want to update the records using checkboxes. I am able to display the database record quite alright and I have created an array of checkboxes, but my update loop is not working. Here is my code: /*---------This retrieves the records -------------*/ if($row_md) { do{ echo "<tr><td text align='right'>";
11
15294
by: TechnoAtif | last post by:
INSERT AND UPDATE MULTIPLE CHECKBOX DATA USING PHPMYSQL OR JAVASCRIPT Hi All I want to check the multiple checkboxes update them after revisiting that page. I am taking the name as name=type..in the form of array..but on checking it the result in the database simply gives the world 'array'.i want multiple checkboxes to be checked and updated simultaneously.. and the query to insert the checkbox value has to be included along with...
4
6500
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be retrieved using list/menu box. When i check only a single checkbox to insert the checked category ,selecting that category through list box gives out the entire data of the user corresponding to that category. However when i check multiple checkboxes and...
5
1792
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in risk of losing some new entries, so I've deceided to create an update thread. This thread is created when the program's start and should (for various reason) run not in sync with the Filemonitor. The Filemonitor event creates an entry in a...
0
9566
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
10003
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...
0
9828
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
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5271
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...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2797
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.