I have a settings page as a part of my game for class and one of the settings fields is a checkbox.
This is my HTML field for the checkbox:
<div class="field">
<label for="allow_robot_name">Allow Robot Name?</label>
<input type="checkbox" name="allow_robot_name" <?php if ($r->allow_robot_name == 1) { echo "checked"; } ?>>Yes
</div>
The issue I am having is when the user checks the checkbox the value in the database changes to 1. However if they then uncheck it and then update the database the checkbox resets to checked and the value doesn't change from 1 to 0.
This is my code for updating the database:
<?php
$records = array();
if (!empty($_POST)) {
if (isset($_POST['site_name'], $_POST['header_text'], $_POST['footer_copyright'], $_POST['default_robot_name'])) {
$site_name = $_POST['site_name'];
$header_text = $_POST['header_text'];
$footer_copyright = $_POST['footer_copyright'];
$default_robot_name = $_POST['default_robot_name'];
if (isset($_POST['allow_robot_name'])) {
$allow_robot_name = 1;
} else {
$allow_robot_name = 0;
}
if (!empty($site_name) && !empty($header_text) && !empty($footer_copyright) && !empty($allow_robot_name) && !empty($default_robot_name)) {
$update = $db->prepare("UPDATE user_settings set site_name = ?, header_text = ?, footer_copyright = ?, allow_robot_name = ?, default_robot_name = ?");
$update->bind_param('sssis', $site_name, $header_text, $footer_copyright, $allow_robot_name, $default_robot_name);
if ($update->execute()) {
header('Location: index.php');
die();
}
}
}
}
Why doesn't the checkbox value update?