我有一个设置页面,作为我上课游戏的一部分,并且其中一个设置字段是一个复选框。
这是复选框的我的HTML字段:
<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>
我遇到的问题是,当用户选中该复选框时,数据库中的值更改为1。但是,如果他们随后取消选中该复选框,然后更新数据库,则该复选框将重置为选中状态,并且该值不会从1更改为0。
这是我用于更新数据库的代码:
<?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();
}
}
}
}
复选框值为什么不更新?
这是由于!empty($allow_robot_name)
。
empty($x)
意味着!isset($x) || $x == null
。由于在PHP中0
被认为等效null
,empty($allow_robot_name)
因此true
,如果未选中此复选框并且不执行更新查询,则结果将为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句