I am trying to get my mysql database to update a value in a column when a checkbox is checked/unchecked. This works for 1 word variable, but somehow refuses when the checkbox that is checked has 2 words (in other words, has a space in the string).
The list of checkboxes in the form is taken from the database which holds the name used for the checkbox name field and the actual text displayed.
<input type="checkbox" class="form-check-input filter_check" value="<?= $row['buttons_allowed_activated']; ?>" name="<?= $row['Service']; ?>" id="labels" <?php if ($activate_status == 1) echo 'checked="checked"'; ?>><?= $row['Service'];?>
This works flawlesly and there are no issues when it comes to values like “My Value” taken from the database Service column.
From there the edit document takes over when the form is posted (as usual) and there I check on every Service (by Service name as it comes from the database in the checkbox fields) if the checkbox was checked or not:
$checkboxes_array = array();
$unset_value = array();
if (isset($_POST['Service1'])) {
$checkboxes_array[] = 'Service1';
} else {
$unset_values[] = 'Service1';
}
if (isset($_POST['My Service2'])) {
$checkboxes_array[] = 'My Service2';
} else {
$unset_values[] = 'My Service2';
}
if (isset($_POST['Service3'])) {
$checkboxes_array[] = 'Service3';
} else {
$unset_values[] = 'Service3';
}
I then loop over them as the idea of this form is to enable/disable Services on the homepage. This is done by looping over both the $unset_values
array as the $checkboxes_array
to know which ones should have the enabled tag (1) set in the database or which ones should be set to disabled (0).
foreach ($checkboxes_array as &$checkbox){
$button_set_results = mysqli_query($conn, "SELECT * FROM buttons_allowed INNER JOIN buttons ON buttons_allowed.buttons_allowed_button_id=buttons.button_id WHERE (buttons_allowed_userid) IN ('".$userId."') AND (Service) IN ('".$checkbox."') ");
while ($rows = mysqli_fetch_array($button_set_results)){
$button_set_service = $rows['Service'];
$update = mysqli_query($conn, "UPDATE buttons_allowed INNER JOIN buttons ON buttons_allowed.buttons_allowed_button_id=buttons.button_id set buttons_allowed_activated = 1 where Service = '".$button_set_service."' and buttons_allowed_userid = '".$userId."' ");
}
}
foreach( $unset_values as &$unset_value ){
$button_unset_results = mysqli_query($conn, "SELECT * FROM buttons_allowed INNER JOIN buttons ON buttons_allowed.buttons_allowed_button_id=buttons.button_id WHERE (buttons_allowed_userid) IN ('".$userId."') AND (Service) IN ('".$unset_value."') ");
while ($rows = mysqli_fetch_array($button_unset_results)){
$button_unset_service = $rows['Service'];
$update = mysqli_query($conn, "UPDATE buttons_allowed INNER JOIN buttons ON buttons_allowed.buttons_allowed_button_id=buttons.button_id set buttons_allowed_activated = 0 where Service = '".$button_unset_service."' and buttons_allowed_userid = '".$userId."' ");
}
}
From what I am seeing, it just doesn’t make sense that I am unable to set the disabled tag (buttons_allowed_activated) to 0 for the strings with a space in them while I am unable to set them as enabled. The code is exactly the same except for the array used and the value set for the buttons_allowed_activated column.
What am I missing here or what am I doing wrong?
I checked if the values in the arrays are correct and in line with the Service column, which they are.
I tried the call in the database itself and that works normally and actually sets the value to 1 as it should:
UPDATE buttons_allowed INNER JOIN buttons ON buttons_allowed.buttons_allowed_button_id=buttons.button_id set buttons_allowed_activated = 1 where Service = 'My Service2' and buttons_allowed_userid = '{user id for testing purposes}'