drewrockshard
asked on
Insert Single Checkbox value into mySQL database
I am trying to insert a single checkbox value into a mySQL database field.
I thought this would be pretty easy, however, I'm finding that is not true. All I want it to do is, if the checkbox is checked, insert a "0" into a mysql table, and if the checkbox IS checked, to insert a "1". It's a single checkbox. I'm using checkboxes because i don't want to use two radio buttons just to portray "yes or no".
Here's the HTML snippet:
<input type="checkbox" id="active" name="active" value="0" />
The above is outputted as a result of a function that checks the database to see if there is a "1" or a "0". So then this is displayed to the user. So in this case, the checkbox un unchecked with a value of "0". What I want to be able to do is check the checkbox, and for it to insert into the database a "1". I want to do this and vise versa. If it is a "1", and I uncheck it, I want to be able to place a "0" into the database.
Getting the current database results is not the problem, it's pushing the changes to the database that are.
Please let me know if you guys need additional info.
I thought this would be pretty easy, however, I'm finding that is not true. All I want it to do is, if the checkbox is checked, insert a "0" into a mysql table, and if the checkbox IS checked, to insert a "1". It's a single checkbox. I'm using checkboxes because i don't want to use two radio buttons just to portray "yes or no".
Here's the HTML snippet:
<input type="checkbox" id="active" name="active" value="0" />
The above is outputted as a result of a function that checks the database to see if there is a "1" or a "0". So then this is displayed to the user. So in this case, the checkbox un unchecked with a value of "0". What I want to be able to do is check the checkbox, and for it to insert into the database a "1". I want to do this and vise versa. If it is a "1", and I uncheck it, I want to be able to place a "0" into the database.
Getting the current database results is not the problem, it's pushing the changes to the database that are.
Please let me know if you guys need additional info.
ASKER
Hmmm.
Is there any way to push the "0" and "1" - or should I go about this another way?
Here's some code:
// Checks to see if the customer is active or not.
function isActiveCustomer() {
global $db;
if ($_POST['customerid'] == "") {
$custID = $_GET['customerid'];
}
else {
$custID = $_POST['customerid'];
}
if ($_POST['customerid'] == "" && $_GET['customerid'] == "") {
$query = "SELECT CustomerActive FROM customers LIMIT 0,1";
}
else {
$query = "SELECT CustomerActive FROM customers WHERE CustomerID='$custID'";
}
$activeCheck = $db->get_row("$query",ARRA Y_A);
if($activeCheck['CustomerA ctive'] == "1") {
echo "value=\"1\" checked";
}
elseif ($activeCheck['CustomerAct ive'] == "0") {
echo "value=\"0\"";
}
}
Previous code snippet actually fills out the rest of the html input element previously shown. In the for, the code is actually this:
<input type="checkbox" id="active" name="active" <?php echo isActiveCustomer(); ?> />
So the function isActiveCustomer() will just add the additional information from the database results and append (or not) the "checked" text.
Now, what I have been doing to update the database is the following, however, it doesn't appear to be working:
function updateBasicInfo() {
global $db;
$active = $_POST['active'];
$query = "UPDATE customers SET CustomerMapsco='$mapsco', CustomerActive='$active', CustomerFirstName='$custFi rstName', CustomerLastName='$custLas tName', Customer2FirstName='$cust2 FirstName' , Customer2LastName='$cust2L astName', CustomerServiceStreet='$se rviceAddr' , CustomerServiceCity='$serv iceAddrCit y', CustomerServiceState='$ser viceAddrSt ate', CustomerServiceZip='$servi ceAddrZip' , CustomerBillingStreet='$bi llingAddr' , CustomerBillingCity='$bill ingAddrCit y', CustomerBillingState='$bil lingAddrSt ate', CustomerBillingZip='$billi ngAddrZip' , CustomerHomePhone='$homePh one', CustomerCellPhone='$cellPh one', CustomerWorkPhone='$workPh one', CustomerWorkExt='$workPhon eExt', CustomerFax='$faxPhone', CustomerEmail='$emailAddr' , CustomerDog='$hasDogs', CrewID='$assignedCrew', ScheduleID='$scheduleNum', AssignedCompanyID='$assign edCompany' , FuelSurcharge='$fuelSurcha rge' WHERE CustomerID='$custID'";
$updateDatabase = $db->query($query);
if ($updateDatabase) { ?>
<p class="success">Customer information was successfully updated.</p>
<?php
} else { ?>
<p class="failed">Failed to update customer information.</p>
<?php }
}
Pay no attention to the other variables reference. Those have been taken out to save space. They all work, I just posted you the code to show what I use to "update" the database - but it doesn't work. It does exactly what you said - it places "on" into the database, and it places nothing (null) if I uncheck it.
Is there any way to push the "0" and "1" - or should I go about this another way?
Here's some code:
// Checks to see if the customer is active or not.
function isActiveCustomer() {
global $db;
if ($_POST['customerid'] == "") {
$custID = $_GET['customerid'];
}
else {
$custID = $_POST['customerid'];
}
if ($_POST['customerid'] == "" && $_GET['customerid'] == "") {
$query = "SELECT CustomerActive FROM customers LIMIT 0,1";
}
else {
$query = "SELECT CustomerActive FROM customers WHERE CustomerID='$custID'";
}
$activeCheck = $db->get_row("$query",ARRA
if($activeCheck['CustomerA
echo "value=\"1\" checked";
}
elseif ($activeCheck['CustomerAct
echo "value=\"0\"";
}
}
Previous code snippet actually fills out the rest of the html input element previously shown. In the for, the code is actually this:
<input type="checkbox" id="active" name="active" <?php echo isActiveCustomer(); ?> />
So the function isActiveCustomer() will just add the additional information from the database results and append (or not) the "checked" text.
Now, what I have been doing to update the database is the following, however, it doesn't appear to be working:
function updateBasicInfo() {
global $db;
$active = $_POST['active'];
$query = "UPDATE customers SET CustomerMapsco='$mapsco', CustomerActive='$active', CustomerFirstName='$custFi
$updateDatabase = $db->query($query);
if ($updateDatabase) { ?>
<p class="success">Customer information was successfully updated.</p>
<?php
} else { ?>
<p class="failed">Failed to update customer information.</p>
<?php }
}
Pay no attention to the other variables reference. Those have been taken out to save space. They all work, I just posted you the code to show what I use to "update" the database - but it doesn't work. It does exactly what you said - it places "on" into the database, and it places nothing (null) if I uncheck it.
It's really helpful to have the code posted in the "attach code snippet" feature - makes it easy to read and gives us line numbers we can all use to refer to the lines.
I'm sure we can translate the values into something useful, but rather than try to go through all of that, let me show you an example that you can install and run. You can watch the way the checkboxes work and then decide if your strategy makes sense for your application.
I'm sure we can translate the values into something useful, but rather than try to go through all of that, let me show you an example that you can install and run. You can watch the way the checkboxes work and then decide if your strategy makes sense for your application.
<?php // RAY_checkboxes.php DEMONSTRATE HOW CHECKBOXES WORK IN FORM AND ACTION SCRIPTS
error_reporting(E_ALL);
// IF ANYTHING HAS BEEN POSTED, SHOW THE CONTENTS
if (!empty($_POST))
{
echo "<pre>";
echo "\$_POST CONTAINS:\n";
var_dump($_POST);
echo "</pre> \n";
}
?>
<form method="POST">
These checkboxes have names...<br/>
Check or Uncheck Some Boxes Here<br/>
<input name=box1 type="checkbox" value="Box1"><br/>
<input name=box2 type="checkbox" value="Box2"><br/>
<input name=box3 type="checkbox"><br/>
<input name=box4 type="checkbox"><br/>
<br />
These checkboxes are part of the "box" array...<br/>
Check or Uncheck Some Boxes Here<br/>
<input name=box[] type="checkbox" value="ABox1"><br/>
<input name=box[] type="checkbox" value="ABox2"><br/>
<input name=box[] type="checkbox" value="ABox3"><br/>
<input name=box[] type="checkbox" value="ABox4"><br/>
<input type="submit" value="go" />
</form>
ASKER
Ray:
So does this mean I am unable to do what I want with this? Please let me know. I understand how this works, however, it doesn't appear that it will do what I want it to do.
Let me know.
So does this mean I am unable to do what I want with this? Please let me know. I understand how this works, however, it doesn't appear that it will do what I want it to do.
Let me know.
ASKER
Nevermind. Using radio buttons now.
I've been tinkering around with this some, and I think you can do exactly what you want, but the PHP script needs to be aware of the checkbox names.
ASKER
Ray,
You can paste the code and I will test it since you worked on it. I would love to use checkboxes, but if not, I can easily use radio buttons. If you decide to post the code and I get it working, then you can reject my solution (as myself) and I'll award the points.
You can paste the code and I will test it since you worked on it. I would love to use checkboxes, but if not, I can easily use radio buttons. If you decide to post the code and I get it working, then you can reject my solution (as myself) and I'll award the points.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm on a time crunch. From your previous update stating the following:
"The value of an unchecked checkbox is simply not there - nothing comes through in $_POST."
Then you posting demostrating how checkboxes work - I figured you were basically saying that it wasn't possible. So thats when I tried something else, and after posting, you said it was possible if you changed some code around. So, thats when I stated I would award points. So, I wanted to get this to work, however, I was under the impression that it wasn't possible. No offence to you.
I'll implement this code as this is what I really wanted. Don't just to conclusions as to how I don't want to put my time to something - I figured you were a PHP guru and by stated your initial thoughts, it sounded like it wasn't possible.
Thanks.
"The value of an unchecked checkbox is simply not there - nothing comes through in $_POST."
Then you posting demostrating how checkboxes work - I figured you were basically saying that it wasn't possible. So thats when I tried something else, and after posting, you said it was possible if you changed some code around. So, thats when I stated I would award points. So, I wanted to get this to work, however, I was under the impression that it wasn't possible. No offence to you.
I'll implement this code as this is what I really wanted. Don't just to conclusions as to how I don't want to put my time to something - I figured you were a PHP guru and by stated your initial thoughts, it sounded like it wasn't possible.
Thanks.
ASKER
This code worked perfectly as to what I was trying to do.
"sounded like it wasn't possible...." For the most part, things are always "possible" in PHP - it's just a matter of how much time you want to put into crafting the solution and whether the design is suitable for a particular purpose.
PHP has over 5,000 functions. One of them is bound to work ;-)
http://us2.php.net/quickref.php
Thanks for the points and good luck with your project, ~Ray
PHP has over 5,000 functions. One of them is bound to work ;-)
http://us2.php.net/quickref.php
Thanks for the points and good luck with your project, ~Ray
The value of an unchecked checkbox is simply not there - nothing comes through in $_POST.
Please post the code you're using now to process the checkbox. Maybe we can help revise it.
Best, ~Ray