作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了一个设置表单。设置表单的链接在这里 -
我已经插入了值。请检查我的数据库。
我想从数据库中检索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/
我是一名优秀的程序员,十分优秀!