PHP checkbox value database

I have added a checkbox to unsubscribe to the php susbcription form. Also have added a new field to the database with the field Name user_out and Type tinyint(1). I am not sure how to pass the checkbox value to the database.

phpformbuilder check box:

$form->addCheckbox('user-out', 'Unsubscribe', 1, '');
$form->printCheckboxGroup('user-out','');

phpformbuilder: the fourth line is my attempt to pass the checkbox value to the database.

        $insert['ID'] = Mysql::SQLValue('');
        $insert['user_name'] = Mysql::SQLValue($_POST['user-name']);
        $insert['user_email'] = Mysql::SQLValue($_POST['user-email']);
        $insert['user_out'] = Mysql::SQLValue($_POST['user-out']);
        if (!$db->insertRow('subscribers', $insert)) {
            $user_message = '<p class="alert alert-danger">' . $db->error() . '<br>' . $db->getLastSql() . '</p>' . "\n";
        } else {
            $user_message = '<p class="alert alert-success">Thanks for subscribing!</p>' . "\n";
            Form::clear('newsletter-suscribe-form');
        }

As i can see you printing checkboxgroup empty ? 'user-out', ' '

You can simply check if checkbox value is 1, then put 1 or 0 instead of $_POST['user-out']

if (isset($_POST['submit'])) {
	if (isset($_POST['user-out']) && $_POST['user-out'] == '1') { // if checkbox value is 1

		/*
		UPDATE database, set 1 to user-out
		if you want to update user-out to be 0 after unsubscribe you can remove $_POST['user-out'] and set it to 0 manually in code
		*/
		$insert['ID'] = Mysql::SQLValue('');
        $insert['user_name'] = Mysql::SQLValue($_POST['user-name']);
        $insert['user_email'] = Mysql::SQLValue($_POST['user-email']);
        $insert['user_out'] = Mysql::SQLValue($_POST['user-out']);	// instead of $_POST['user-out'] put '0'
        if (!$db->insertRow('subscribers', $insert)) {
            $user_message = '<p class="alert alert-danger">' . $db->error() . '<br>' . $db->getLastSql() . '</p>' . "\n";
        } else {
            $user_message = '<p class="alert alert-success">Thanks for subscribing!</p>' . "\n";
            Form::clear('newsletter-suscribe-form');
        }
	}
}

Yes this is the right idea. I do get a warning error when running the form with the checkbox checked.
Warning: strlen() expects parameter 1 to be string, array
Not sure if the values I entered for the field user_out in the database table are correct.
Field: user_out
Type: tinyint(1)
Null: Yes
Defaul: NULL

You need to check this, you are passing array to strlen() function, you don’t need to check string length for checkbox.

For this type you need only 1 and 0, 0 by default and 1 when user unsubscribe.

Can you post code block where you checking for inputs and form ?

I renamed user-out to opt-out.

Database insert:
$db = new Mysql();
$insert[‘ID’] = Mysql::SQLValue(‘’);
$insert[‘user_name’] = Mysql::SQLValue($_POST[‘user-name’]);
$insert[‘user_email’] = Mysql::SQLValue($_POST[‘user-email’]);

    // checkbox
    if(isset($_POST['opt-out'])) {
        $insert['opt_out'] = Mysql::SQLValue($_POST['opt-out']);
    } else {
        $insert['opt_out'] = Mysql::SQLValue('');
    } 

Form checkbox:
$form->addCheckbox(‘opt-out’, ‘Unsubscribe’, 1, ‘’);
$form->printCheckboxGroup(‘opt-out’, ‘’);

Change to if(isset($_POST['opt-out']) && $_POST['opt-out'] == '1') to check if checkbox value is 1

Next add on top of script error_reporting(E_ALL); and var_dump($_POST); try to see what you get with $_POST when you submit your form, it will point you to right direction and throw you actual error.

I get this error:
array(6) { [“newsletter-suscribe-form-token”]=> string(32) “5329000685a384b97cabd50.77273465” [“newsletter-suscribe-form”]=> string(1) “1” [“user-name”]=> string(4) “PASE” [“user-email”]=> string(22) “pablo@pabloserrano.com” [“opt-out”]=> array(1) { [0]=> string(1) “1” } [“submit-btn”]=> string(1) “1” }

form code

    error_reporting(E_ALL);
    var_dump($_POST);

    $db = new Mysql();
    $insert['ID'] = Mysql::SQLValue('');
    $insert['user_name'] = Mysql::SQLValue($_POST['user-name']);
    $insert['user_email'] = Mysql::SQLValue($_POST['user-email']);
            
    if(isset($_POST['opt-out']) && $_POST['opt-out'] == '1') {
        $insert['opt_out'] = Mysql::SQLValue($_POST['opt-out']);
    } else {
        $insert['opt_out'] = Mysql::SQLValue('');
    }

I don’t see an error message there. What I see is the result of this line:

Actually, I am not getting an error any longer. The opt_out field in the database only records NULL regadles if the checkbox is checked instead of 1 or 0;

Just a thought. Are you sure the dash names aren’t getting mixed up with the underscore names?

I doubled checked and they look good to me. The field name is opt_out and the checkbox name opt-out.

    if(isset($_POST['opt-out']) && $_POST['opt-out'] == '1') {
        $insert['opt_out'] = Mysql::SQLValue($_POST['opt-out']);
    } else {
        $insert['opt_out'] = Mysql::SQLValue('');
    }
1 Like

If this is Unsubscribe form, you don’t need ELSE in your statement because you only checking if checkbox is checked, and updating column opt_out with 1.

Is this a update or insert query ?

The if statement is not producing errors anymore. The form is meant as a signup feature. The checkbox is and additional option in the form for subscribers that might want to unsubscribe. The value of the opt_out filed in the database s alway NULL regardless if the checkbox is checked or unchecked. Instead it should be 1 or 0.

link ot test form: http://betweenartandlight.com/subscribe-test05.php

Change in database those options Name : opt_out, Type : tinyint(1), Null: No, Default : 0 this will set default value to 0, and for unsubscribe will be set to 1.

And change

to

// if checkbox is checked, opt_out will be set 1 if unsubscire, otherwive it will be set to 0 as default
if(isset($_POST['opt-out']) && $_POST['opt-out'] == '1') {
	$insert['opt_out'] = Mysql::SQLValue($_POST['opt-out']);
}

That’s confused me a few times in looking at this code. Not sure why the form name and field name aren’t consistent, it would surely be easier.

Well, this explains the strlen() error - you can see that where everything else is being passed as a string, the opt-out field is being passed as an array. The only question now is, why? OK, this is the html code from your form, and this is why it comes in as an array:

<input type="checkbox" id="opt-out_0" name="opt-out[]" value="1"  class="form-check-input">Unsubscribe

This section from the documentation for PHP Form builder says

  * Adds checkbox to $group_name
    *
    * @param string $group_name The checkbox groupname (will be converted to an array of indexed value)

that the checkbox will always be converted to an array, so it looks like you don’t have much choice but to refer to it as:

$_POST['opt-out'][0]

when you want to use it in your PHP code.

1 Like

Field: opt_out
Type: tinyint(1)
Null: No
Default: 0

Produces this error:

Column ‘opt_out’ cannot be null (#1048)
INSERT INTO subscribers (ID, user_name, user_email, opt_out) VALUES (NULL, ‘TEST2’, ‘test@pabloserrano.com’, NULL)

=============

Field: opt_out
Type: tinyint(1)
Null: Yes
Default: NULL

Produces no errors, but the opt_out values is always NULL in the database

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.