Inserting Checkbox value into mysql db from a form

It doesn’t appear to be an issue. I actually got it to work finally. Instead of:
if (isset($_GET['variable_check ']) && ($_GET['variable_check '] == “1”)) { $query .= "variable_check = 1 "; } else { $query .= "variable_check = 0 "; }

I removed the == changed it to = it seemed to do the trick tho I’m not sure if it will have some reprecussions.
if (isset($_GET['variable_check ']) && ($_GET['variable_check '] = “1”)) { $query .= "variable_check = 1 "; } else { $query .= "variable_check = 0 "; }

I imagine it will.
Using a single = means you are no longer making a comparison, you are assigning a value, so is likely to return true no matter what making that part of the if condition virtually useless.

1 Like

It is an issue. I just tried a small piece of code:

if ($_GET['test '] == "Y") echo "found";

and when I run it, I get

( ! ) Notice: Undefined index: test in C:\wamp\www\sample\testconnect.php on line 14

when I supply a parameter called “test” in the url. If I remove the space, I don’t get the warning.

1 Like

Looking again, that’s why it works. You may as well delete the && ($GET['variablecheck '] = "1") bit and just have:-

if (isset($GET['variablecheck '])) { $query .= "variable_check = 1 "; } else { $query .= "variable_check = 0 "; }

Which is not a million miles from my initial post #2:-

if(isset($_POST['variable_check'])) { $varcheck = 1; }
else { $varcheck = 0; }

failed to spot you use get not post

Took me forever to get the checkbox to work…
Besides all the mentioned spaces around keys the main thing I noticed was no matter what value you put for the checkbox value, when viewing the source it would always be empty. The value was being overwritten by the line below (line100) found on webapp.js.

$('#form_record #variable_check').val('');

After some searching I came up with this.

$('#form_record #variable_check').output='<input type="checkbox" name="variable_check" id="variable_check" value="1">';

I found it interesting that this rewrites the form line altogether.

Around line 95 of data.php, I changed the script to look for “1”.

if (isset($_GET['variable_check']) && $_GET['variable_check'] == "1") { $query .= "variable_check = '1' "; } else { $query .= "variable_check = '0' "; }

Now if checkbox is checked the value 1 is saved to DB. If not checked, 0 is saved. You could probably reverse those values if you wanted zero saved when checked. At least I got it to work.

2 Likes

Works great thank you so much for all your efforts!

I’ve added a ‘Datetime’ field that pulls the correct date from the DB no problem but it won’t add it when putting in a new record or edit it, it just comes out as 0000-00-00 00:00:00 every time. I currently have the following code for it.

In the webapps.php I have

$('#form_record #date').val(output.data[0].date);

And data.php

if (isset($_GET['date'])) { $query .= "date = '" . mysqli_real_escape_string($db_connection, $_GET['date'])							. "', "; }

While Index.php has

<div class="input_container">
   <label for="date">Date: </label>
<div class="field_container">
  <input type="datetime" name="datetime" id="date" value="">
</div>
</div>

if that field is supposed to hold the timestamp when the record was added/updated, you can leave that to MySQL: https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

1 Like

Unfortunately it’s not that easy. The user needs to be able to enter the date and time as it will change.

run the date through DateTime and make it return the properly formatted date string.

http://php.net/datetime

I will give that a shot, thanks.

I had to add 2 more check boxes and a few other text fields. I found that if I add fields in the data.php query that are after the checkbox coding it makes the form bomb when submitted. Therefore I moved the variable_check coding to the end of the query and problem solved. However, now that I have to add 2 more check boxes (varchk_2, varchk_3) I can’t do them the same way as nothing seems to run after it gets to the first piece of checkbox code (variable_check). I apologize, I didn’t realize I needed more than one checkbox. Thanks.

First check is to see that you are adding commas into the query to separate the fields where you need to, and not adding them where you should not. Note that as long as the syntax is correct, it doesn’t matter what order you put the columns in the query.

Currently it’s set up as follows with 2 checkboxes at the end?

if (isset($_GET['total_cost']))  { $query .= "total_cost = '" 	. mysqli_real_escape_string($db_connection, $_GET['total_cost']) . "', "; }	
if (isset($_GET['total_qty_smt_components'])) 	 { $query .= "total_qty_smt_components = '" . mysqli_real_escape_string($db_connection, _GET['total_qty_smt_components'])	 . "', "; }	
// ---------------------------------------------          CheckBoxes           ----------------------------------------------
if (isset($_GET['varchk_1']) && $_GET[' varchk_1'] == 1)  { $query .= " varchk_1 = 1 "; } else { $query .= " varchk_1 = 0 ". "', "; }
if (isset($_GET['variable_check']) && $_GET['variable_check'] == 1)  { $query .= "variable_check= 1 "; } else { $query .= "variable_check= 0 "; }

See after the first checkbox, varchk_1, you have a comma (though only if you set it to 0, strangely). You need a comma between fields, so this would be correct:

varchk_1 = 0 , variable_check = 1

but this is not

varchk_1 = 1 variable_check = 1

So I would expect with that code, you’ll get a problem if the first one is set to 1. But either way, adding more check boxes would be just

if (isset($GET['varchk1']) && $GET['varchk1'] == 1) { $query .= " varchk_1 = 1, "; } else { $query .= " varchk_1 = 0, "; }
if (isset($GET['varchk2']) && $GET['varchk2'] == 1) { $query .= " varchk_2 = 1, "; } else { $query .= " varchk_2 = 0, "; }
if (isset($GET['varchk3']) && $GET['varchk3'] == 1) { $query .= " varchk_3 = 1, "; } else { $query .= " varchk_3 = 0, "; }
if (isset($GET['variablecheck']) && $GET['variablecheck'] == 1) { $query .= "variable_check= 1 "; } else { $query .= "variable_check= 0 "; }

Worked perfect, thanks so much I’m not sure I would’ve caught that so quickly.

Hello again,
Depending on the last working solution for you (by @droopsnoot ) and just for curiosity,
are you using (or planning to use) the values from these check boxes anywhere in your code/query?
in other words, can a checkbox have value other than 1 ?
if so, why not doing it as simple as this

$query="INSERT INTO DB_Table1  SET ";

$checkArr=array();
$checkArr[]="varchk_1 = ". (isset($GET['varchk1'])?$GET['varchk1']:0);
$checkArr[]="varchk_2 = ". (isset($GET['varchk2'])?$GET['varchk2']:0);
$checkArr[]="varchk_3 = ". (isset($GET['varchk3'])?$GET['varchk3']:0);
$checkArr[]="variablecheck = ". (isset($GET['variablecheck'])?$GET['variablecheck']:0);

$query .= implode(", ", $checkArr);

If you just want to know whether or not a user ticked off a boxes regardless of its assigned value (toggling on/off true/false, Boolean like values), the code will be much shorter then

$query="INSERT INTO DB_Table1  SET ";

$checkArr=array();
$checkArr[]="varchk_1 = ". (isset($GET['varchk1'])?1:0);
$checkArr[]="varchk_2 = ". (isset($GET['varchk2'])?1:0);
$checkArr[]="varchk_3 = ". (isset($GET['varchk3'])?1:0);
$checkArr[]="variablecheck = ". (isset($GET['variablecheck'])?1:0);

$query .= implode(", ", $checkArr);

I’m not forcing you to do it this way but I see it easier to code :slight_smile:

Regards

Currently it’ll just be used for 1 or 0 (checked or unchecked respectively) is there a benefit to doing it your way over droopsnoot’s or is it just a different way to go? Thanks.

First of, I have to mentions a typo I made in my previous post
I typed $GET instead of $_GET, so please make sure to change it before you test it

Now, Since it’s a 0/1 check only (where 1 means checked) then

  1. there is no need to test your varchk# against (1) and you only check if the varchk# was sent (note that if a checkbox is not ticked off it will not be sent to the server),
  2. also there will be no need to add value=“1” in your html code which makes you write a lesser html code
  3. the php code also will be shorter and more reliable when building the sql query (by pushing column/value items to the chekArr then join them with the proper separator ", " so you don’t miss one or add extra one at the end of the query)

again, it’s just another way to do it.
Regards

Thanks for explanation, it’s always good to know other ways to do things.