gpt4 book ai didi

php - 使用 mysql 和 php 的复选框更新多行

转载 作者:行者123 更新时间:2023-11-29 21:42:36 27 4
gpt4 key购买 nike

我已经创建了一个设置表单。设置表单的链接在这里 -

enter image description here

我已经插入了值。请检查我的数据库。

enter image description here

我想从数据库中检索option_value,并将其作为值填充到复选框中。

当有“Y”时,复选框将被选中。然后我想将其更改为“N”,它将更新为option_value字段,反之亦然。

更新查询将影响单个元素或多个元素。

有人可以帮我吗?我怎样才能实现这个目标?

最佳答案

这是一个基本的模型(可能无法完全工作),但您将了解代码的想法。

-> 获取所有数据库选项并根据该选项创建复选框。
-> 检查数据库并尝试查找复选框的更改值并进行相应更新。

更多信息在代码中注释。

显示复选框的文件:

<?php
$result = mysqli_query($connection, "SELECT option_name, option_value FROM mytable");
//Gets all the options from the database, so the values have to be in there in the first place.
while($row = mysqli_fetch_assoc($result))
//Loops through each value in the database
{?>
<?php if($row['option_value'] == 'Y')
{
?>
<input type='checkbox' name="<?php echo $row['option_name']; ?>" checked>
<?php
//Adds a checked checkbox if the value is "Y"
}
else {
?>
<input type='checkbox' name="<?php echo $row['option_name']; ?>">
<?php
//Adds a unticked checkbox if the value is not "Y" e.g. "N"
}
}
?>

复选框提交到的文件(表单发布到的页面)

<?php

$result = mysqli_query($connection, "SELECT option_name, option_value FROM mytable");
//Gets all of the option names and values
while($row = mysqli_fetch_assoc($result))
{
if(isset($_POST[$row['option_name']]) && $row['option_value'] == "N")
{
//If the option is now yes (isset checks returns true if the box is selected) and the option in the db is N then update.
mysqli_query($connection, "UPDATE mytable SET option_value = 'Y' WHERE option_name ='" . $row['option_name'] . "'");
}
if(!isset($_POST[$row['option_name']]) && $row['option_value'] == "Y")
{
//If the option is now no (isset checks returns false if the box is not selected) and the option in the db is Y then update.
mysqli_query($connection, "UPDATE mytable SET option_value = 'N' WHERE option_name ='" . $row['option_name'] . "'");
}
}

?>

关于php - 使用 mysql 和 php 的复选框更新多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com