在php中使用多复选框更新表
我在表单中有多个复选框,当单击“提交”按钮时,数据库中的表将更新,当选中该复选框时,该代码是正确的,但当未选中该复选框时,表将不会更新在php中使用多复选框更新表,php,Php,我在表单中有多个复选框,当单击“提交”按钮时,数据库中的表将更新,当选中该复选框时,该代码是正确的,但当未选中该复选框时,表将不会更新 <form method="post"> <?php $qr2=$mysqli->query("SELECT `action_id`,`enable` FROM `role`"); for($i=0;$row2=$qr2->fetch_object();$i++){ ?> <input type="che
<form method="post">
<?php
$qr2=$mysqli->query("SELECT `action_id`,`enable` FROM `role`");
for($i=0;$row2=$qr2->fetch_object();$i++){
?>
<input type="checkbox" <?php if($row2->enable == '1'){ ?> checked <?php } ?> id="upc" name="upc[]" value="<?php echo $row2->action_id; ?>">
<?php
}
?>
<input type="submit" id="up" name="up" value="update">
<?php update_role(); ?>
</form>
<?php
function update_role(){
if(isset($_POST['up'])){
foreach($_POST['upc'] as $check) {
if(isset($_POST['upc'])) {
$mysqli->query("UPDATE `role` SET `enable`='1' WHERE `action_id`='$check'");
}
else{
$mysqli->query("UPDATE `role` SET `enable`='0' WHERE `action_id`='$check'");
}
}
}
}
?>
选中id=“upc”name=“upc[]”值=“”>
选中id=“upc”name=“upc[]”值=“”>
根据您上次的评论,我认为您希望使用所选框中的所有值更新数据库,即使这些值未被选中(由用户取消选择)
我用一个例子更新了我的答案。你可以用很多方法来做到这一点,这只是一个你如何做到这一点的例子
<html>
<head></head>
<body>
<form method="post">
<?php
// Configure your database connection
$servername = "";
$username = "";
$password = "";
$dbname = "";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$role = update_role();
foreach ($role as $actionId => $enable) {
echo sprintf(
'<input type="checkbox" %s id="upc" name="upc[]" value="%s">',
$enable == '1' ? 'checked="checked"' : "",
$actionId
);
}
?>
<input type="submit" id="up" name="up" value="update">
</form>
<?php
/**
* Return an array which contains the configured role settings.
* In the format array($actionId => $enable)
*
* For example:
*
* array(
* 1 => '0',
* 422 => '1'
* );
*
* @return array
*/
function update_role()
{
global $mysqli;
$qr2 = $mysqli->query("SELECT `action_id`,`enable` FROM `role`");
$role = [];
// Put the role from the database in an array.
for ($i=0; $row2=$qr2->fetch_object(); $i++) {
$role[$row2->action_id] = $row2->enable;
}
if (isset($_POST['up'])) {
$isUpcSelected = isset($_POST['upc']) ? true : false;
// Loop the role array and make the default 'enable' value '0' because on the server you will only receive
// which checkboxes are selected in the $_POST array. This means that $isUpcSelected can be false on a POST.
// But we also want to update the select boxes that are not selected/deselected, which they are now by default.
foreach ($role as $actionId => $enable) {
$role[$actionId] = '0';
}
// Now if there are selected checkboxes, then get them from the $_POST array and set their value to '1'.
if ($isUpcSelected) {
foreach ($_POST['upc'] as $checked) {
if (array_key_exists($checked, $role)) {
$role[$checked] = '1';
}
}
}
// Then update the database with the values from the $role array.
foreach ($role as $actionId => $enable) {
$mysqli->query("UPDATE `role` SET `enable`='$enable' WHERE `action_id`='$actionId'");
}
}
return $role;
}
?>
</body>
</html>
tnx!如果没有选中multi-checkbox,获取值的解决方案是什么?@FatemehNamkhah如果我正确理解了您的问题,您想知道multi-selectbox是否被选中并为此创建一个变量吗?在这种情况下,您可以检查['upc']的$\u POST数组,并为其创建一个布尔变量:$isUpcSelected=isset($\u POST['upc'])?真:假;否。我想将其保存到数据库:选中复选框->启用(&C)复选框未选中->禁用
<html>
<head></head>
<body>
<form method="post">
<?php
// Configure your database connection
$servername = "";
$username = "";
$password = "";
$dbname = "";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$role = update_role();
foreach ($role as $actionId => $enable) {
echo sprintf(
'<input type="checkbox" %s id="upc" name="upc[]" value="%s">',
$enable == '1' ? 'checked="checked"' : "",
$actionId
);
}
?>
<input type="submit" id="up" name="up" value="update">
</form>
<?php
/**
* Return an array which contains the configured role settings.
* In the format array($actionId => $enable)
*
* For example:
*
* array(
* 1 => '0',
* 422 => '1'
* );
*
* @return array
*/
function update_role()
{
global $mysqli;
$qr2 = $mysqli->query("SELECT `action_id`,`enable` FROM `role`");
$role = [];
// Put the role from the database in an array.
for ($i=0; $row2=$qr2->fetch_object(); $i++) {
$role[$row2->action_id] = $row2->enable;
}
if (isset($_POST['up'])) {
$isUpcSelected = isset($_POST['upc']) ? true : false;
// Loop the role array and make the default 'enable' value '0' because on the server you will only receive
// which checkboxes are selected in the $_POST array. This means that $isUpcSelected can be false on a POST.
// But we also want to update the select boxes that are not selected/deselected, which they are now by default.
foreach ($role as $actionId => $enable) {
$role[$actionId] = '0';
}
// Now if there are selected checkboxes, then get them from the $_POST array and set their value to '1'.
if ($isUpcSelected) {
foreach ($_POST['upc'] as $checked) {
if (array_key_exists($checked, $role)) {
$role[$checked] = '1';
}
}
}
// Then update the database with the values from the $role array.
foreach ($role as $actionId => $enable) {
$mysqli->query("UPDATE `role` SET `enable`='$enable' WHERE `action_id`='$actionId'");
}
}
return $role;
}
?>
</body>
</html>