473,386 Members | 1,706 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,386 software developers and data experts.

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...delete 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($_POST['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("SELECT 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_comments (username, creation_stamp, id_people, id_ministry) ".
"VALUES ('$_SESSION[valid_user]', '$datetime', '$_POST[id]',
'$id')";

$mysql_insert = mysql_query($insert, $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_rows($query) == "1") && ($id == )) {

$deletequery = mysql_query("DELETE 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 2295
Rik
Aaron Reimann wrote:
Here is my code:
if (is_array($_POST['commentsid'])) {

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

$query = mysql_query("SELECT 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_comments (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_array() loop).
2. Make sure it's the same format as your $_POST array.
3. array_walk(array_name,'intval') to make sure you have all integers.
4. $to_be_deleted = array_dif($available_array,$post_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_escape_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...delete 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($_POST['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("SELECT 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_comments (username, creation_stamp, id_people, id_ministry) ".
"VALUES ('$_SESSION[valid_user]', '$datetime', '$_POST[id]',
'$id')";

$mysql_insert = mysql_query($insert, $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_rows($query) == "1") && ($id == )) {

$deletequery = mysql_query("DELETE 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
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...
8
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
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....
10
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...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
1
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...
14
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...
13
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.