473,769 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Updating checkboxes / Checking what was checked

I have a lot of check boxes. This is an update of the check boxes, I
want something was checked, then to do an insert (which is currently
working), if something is no longer checked...delet e the checkbox
join/link that is in the database.

So, the insert/checked is working, but the "unchecking " is not working.
I don't know how to compare an array of what was not checked.

Here is my code:
if (is_array($_POS T['commentsid'])) {

foreach ($_POST['commentsid'] as $id) {

##########
## Finding out if it was previously checked
## if not, insert the data

##checking if this $id is already in the database
$query = mysql_query("SE LECT id_ministry FROM join_comments WHERE
id_ministry = '$id'")
or die("Bad query: ".mysql_error() );

## if it is not in the database, insert the id
if (mysql_num_rows ($query) == "0") {
$insert =
"INSERT INTO ".
"join_comme nts (username, creation_stamp, id_people, id_ministry) ".
"VALUES ('$_SESSION[valid_user]', '$datetime', '$_POST[id]',
'$id')";

$mysql_insert = mysql_query($in sert, $mysql_link)
or die("Bad query: ".mysql_error() );

######
### i don't think this needs to be here. i think my delete needs to
be before everything
} elseif ((mysql_num_row s($query) == "1") && ($id == )) {

$deletequery = mysql_query("DE LETE FROM join_comments WHERE
id_people = '$_POST[id]' AND id_ministry ='$id'")
or die("Bad query: ".mysql_error() );
##
######
} else {
print "Error";
}
##
##########

}
}

I think I need to do my delete before everything. He is an "english"
version of what I think needs to be done:

do a query selected all that is in the database
compare what was checked this time against was is checked now
if something is no longer checked {
delete from database the ones that are not in the database now
}
}

I hope this makes sense.

Thank you for any help,
aaron

Oct 18 '06 #1
2 2319
Rik
Aaron Reimann wrote:
Here is my code:
if (is_array($_POS T['commentsid'])) {

foreach ($_POST['commentsid'] as $id) {

$query = mysql_query("SE LECT id_ministry FROM join_comments WHERE
id_ministry = '$id'")
or die("Bad query: ".mysql_error() );
Euhm, security? I'd use an intval($id) or something to be sure it's not a
sql-injection.
## if it is not in the database, insert the id
if (mysql_num_rows ($query) == "0") {
$insert =
"INSERT INTO ".
"join_comme nts (username, creation_stamp, id_people, id_ministry) ".
"VALUES ('$_SESSION[valid_user]', '$datetime', '$_POST[id]',
'$id')";
You do know you don't HAVE to concate?
$insert = "INSERT INTO
join_comments (username, creation_stamp, id_people, id_ministry)
VALUES
('$_SESSION[valid_user]', '$datetime', '$_POST[id]','$id')";

Will work just fine, and saves some useless overhead.
I think I need to do my delete before everything. He is an "english"
version of what I think needs to be done:

do a query selected all that is in the database
compare what was checked this time against was is checked now
if something is no longer checked {
delete from database the ones that are not in the database now
}
}

I hope this makes sense.
1. Create an array of available id's from you database (mysql_query(),
mysql_fetch_arr ay() loop).
2. Make sure it's the same format as your $_POST array.
3. array_walk(arra y_name,'intval' ) to make sure you have all integers.
4. $to_be_deleted = array_dif($avai lable_array,$po st_array).
5. foreach($to_be_ deleted) loop delete.

If I see your code now, I'd say that you might benifit from some protection
from SQL-injections. Loop up the subject on google, expacially
mysql_real_esca pe_string() etc.

Never, ever, trust userdata, not even when they're logged in, trusted
users.
--
Grtz,

Rik Wasmus
Oct 18 '06 #2
Aaron Reimann wrote:
I have a lot of check boxes. This is an update of the check boxes, I
want something was checked, then to do an insert (which is currently
working), if something is no longer checked...delet e the checkbox
join/link that is in the database.

So, the insert/checked is working, but the "unchecking " is not working.
I don't know how to compare an array of what was not checked.

Here is my code:
if (is_array($_POS T['commentsid'])) {

foreach ($_POST['commentsid'] as $id) {

##########
## Finding out if it was previously checked
## if not, insert the data

##checking if this $id is already in the database
$query = mysql_query("SE LECT id_ministry FROM join_comments WHERE
id_ministry = '$id'")
or die("Bad query: ".mysql_error() );

## if it is not in the database, insert the id
if (mysql_num_rows ($query) == "0") {
$insert =
"INSERT INTO ".
"join_comme nts (username, creation_stamp, id_people, id_ministry) ".
"VALUES ('$_SESSION[valid_user]', '$datetime', '$_POST[id]',
'$id')";

$mysql_insert = mysql_query($in sert, $mysql_link)
or die("Bad query: ".mysql_error() );

######
### i don't think this needs to be here. i think my delete needs to
be before everything
} elseif ((mysql_num_row s($query) == "1") && ($id == )) {

$deletequery = mysql_query("DE LETE FROM join_comments WHERE
id_people = '$_POST[id]' AND id_ministry ='$id'")
or die("Bad query: ".mysql_error() );
##
######
} else {
print "Error";
}
##
##########

}
}

I think I need to do my delete before everything. He is an "english"
version of what I think needs to be done:

do a query selected all that is in the database
compare what was checked this time against was is checked now
if something is no longer checked {
delete from database the ones that are not in the database now
}
}

I hope this makes sense.

Thank you for any help,
aaron
I think you are falling over one of the AWFUL AWFUL features of HTML
forms: an unchecked check-box is simply not returned at all.

If you know about specific boxes on your form, you can take the absence
of a box-name in the arguments as 'unset' for that box. But if you are
looping through a set of boxes, unset ones simply won't be there in your
list at all.

You either need to keep a list of all the boxes you expect, so after you
have seen the arguments from all the checked ones you know the rest are
unchecked; or if it is not practical or convenient for the script to
determine what boxes should have been on the page, you can output a
hidden control with some known value for each box, and then loop through
the hidden values seeing whether the value for the corresponding check
box is present.

Colin
Oct 18 '06 #3

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

Similar topics

2
3965
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...
8
21438
by: linuxnooby | last post by:
Hi This is not worlking for me, I want all the checkboxes to be checked by default, what is wrong? <body onload="document.getElementsByTagName('input').checked=true"> cheers Dave
10
5207
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this CheckBox. The CheckBox in the Header exists within the HeaderTemplate of a TemplateColumn in the...
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'>";
0
3120
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td colspan="2"><p>
1
4519
Frinavale
by: Frinavale | last post by:
I'm working on an ASP.NET application using VB.NET for server side code. I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAll". If this checkbox is checked, all of the rows in the GridView are checked, if this checkbox is unchecked, all of the rows in the GridView are unchecked. This checking/unchecking of the checkboxes in the GridView is handled by a JavaScript method: function...
14
3658
by: zufie | last post by:
I have to create a QA report regarding callers calling into a phone hotline. The report consists of many checkboxes such as: Did the IBCCP agency contact you? Yes/NO How many days passed before you heard from the agency? 1Week/2Weeks/3 or More Weeks Did you become an IBCCP client? Yes/NO/Dont Know
13
3092
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes (code pasted below). But if name of the checkbox array is 'chkbx_ary' then it's failing. I want the name to be 'chkbx_ary' because I want to access this array at server side. Though one may not require to see php part but I'm still pasting...
1
5184
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes (code pasted below). But if name of the checkbox array is 'chkbx_ary' then it's failing. I want the name to be 'chkbx_ary' because I want to access this array at server side. Though one may not require to see php part but I'm still pasting...
0
9423
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,...
1
9994
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
9863
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
8870
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
7408
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
6673
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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
2815
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.