[英]Update multiple rows using checbox with mysql and php
I have already created a settings form. 我已经创建了一个设置表单。 The link of the settings form is here -
设置表单的链接在这里-
I have already insereted value. 我已经插入了价值。 Please check my database.
请检查我的数据库。
I want to retrieve the option_value from the database and populate in the checkbox as value. 我想从数据库中检索option_value并在复选框中填充为value。
When there is "Y" then the checkbox will be checked. 如果为“ Y”,则将选中该复选框。 Then I want to change it as "N" and it will update to the option_value field and vice versa.
然后,我想将其更改为“ N” ,它将更新为option_value字段,反之亦然。
The update query would be affect single element or multiple element. 更新查询将影响单个元素或多个元素。
Would anybody please help me how can I acheive this? 谁能帮我这个忙吗?
You should try somethings before ask to stackoverflow, for this time I help to you. 您应该先尝试一些事情,然后再请求stackoverflow,这一次我可以为您提供帮助。
You can do it easily like below: 您可以像下面这样轻松地进行操作:
$options = $db::query('SELECT * FROM options');
echo "<form name='yourFormName' method='post'>";
for ($i=1; $i < count($options); $i++) {
if($options[$i]->option_value == 'Y') {
echo "<input type='text' name='".$options[$i]->option_id."' checked='checked'>";
} else {
echo "<input type='text' name='".$options[$i]->option_id."' checked='checked'>";
}
}
echo "<input type='submit'></form>";
Here is a basic mockup (may not be fully working) but you'll get the idea of the code. 这是一个基本的模型(可能无法完全正常工作),但是您会了解代码的想法。
-> Grabs all db options and makes checkboxes based off that. ->抓住所有的数据库选项,并以此为基础创建复选框。
-> Checks the database and tries to find changed values of checkboxes and updates accordingly. ->检查数据库,并尝试查找复选框的更改值并进行相应更新。
More information is commented in the code. 在代码中注释了更多信息。
File showing the checkboxes: 显示复选框的文件:
<?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"
}
}
?>
File in which the checkboxes are submitted to (the page the form posts to) 提交复选框的文件 (表单发布到的页面)
<?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'] . "'");
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.