提问人:WillWalsh 提问时间:5/26/2023 最后编辑:WillWalsh 更新时间:5/26/2023 访问量:28
使用$row的初始布尔值设置切换开关,在保存/更新时更新布尔
Use Initial Bool Value from $row to Set Toggle Switch, Update Bool on Save/Update
问:
- 我有一个索引页,列出了表格中的所有项目,并带有单独的按钮来查看、编辑和删除该行。< - 工作很棒!
- 在编辑时,我得到了一个带有字段文本框的模式,并且可以编辑和保存更改< - 效果很好!
- 我添加了一个带有“活动”和“非活动”文本的切换开关,可以在单击<时切换 - 效果很好!
- 我现在想在更新其他字段时保存活动/1 或非活动/0 的切换开关“状态”< - 尝试了一些选项,但没有任何效果
- 问题是我不掌握这个过程。数据库确实有一个设置为布尔的“status”字段。
开始切换开关代码 -
<form method="POST" action="inventory_edit.php?inv_id=<?php echo $row['inv_id'];?>"> <div class="mb-3 row"> <label class ="col-sm-3 col-form-label text-light font-weight-bolder text-right">STATUS</label> <div class="col-sm"> <!-- status toggle switch code - changes color and label text on change --> <label class="switch"> <input type="checkbox" id="togBtn"> <div class="slider round"> <span class="off">ACTIVE</span> <span class="on">INACTIVE</span> </div> </label> </div> </div>
结束拨动开关代码 -
启动 PHP 更新代码 -
<?php
session_start();
require_once './includes/favicon.php';
include_once('./includes/dbconn.php');
if(isset($_POST['inventory_edit'])){
$database=new Connection();
$db=$database->open();
try{
$inv_id=$_GET['inv_id'];
$inv_item_status=$_POST['inv_item_status'];
$inv_item_make=$_POST['inv_item_make'];
$inv_item_model=$_POST['inv_item_model'];
$inv_item_sn=$_POST['inv_item_sn'];
$sql="UPDATE inventory SET inv_item_status = '$inv_item_status', inv_item_make = '$inv_item_make', inv_item_model = '$inv_item_model', inv_item_sn = '$inv_item_sn' WHERE inv_id = '$inv_id'";
// if-else statement in executing query
$_SESSION['message']=($db->exec($sql))?'Inventory Item Updated Successfully!'
:'ERROR: Inventory Item Not Updated. (PANTHER Error #IE101)';
}
catch(PDOException $e){
$_SESSION['message']=$e->getMessage();
}
// close database connection
$database->close();
}
else{
$_SESSION['message']='All Fields Required!';
}
header('location: inventory_index.php');
答:
0赞
WillWalsh
5/26/2023
#1
我知道这很简单,我想多了。除了需要将 sql 代码更改为 pdo 之外,真正的问题是我从未初始化用于设置切换开关的变量。我总是“关闭”了 1 条记录(如果前 5 条是 1/true,第 6 条是 0/false,第 7 条是第 1 条 6 条是真,第 7 条是假的)。无论如何,在使用它之前,我用表布尔值初始化了变量。
评论