我需要一些帮助来切换小型 MySQL 数据库中字段中的 bool 值。我创建了一个显示名称和存在字段(Present/Away)的 index.php。为此,我使用 bool 值。该值是可点击的,这应该切换 bool 值。相关代码在这里:
$id=$_GET['id'];
$qresult = mysqli_query($con,"SELECT Present FROM personnel WHERE Inlogcode='$id'");
$result=mysql_fetch_array($qresult);
$presence = (bool)$result;
echo "ID is: " .$id . "<BR>";
echo "Boolean res is: " . $presence . "<BR>";
if ($presence)
{
$sql="UPDATE personnel SET Present=True WHERE Inlogcode='$id'";
$resultaat=mysql_query($sql);
}
else
{
$sql="UPDATE personnel SET Present=False WHERE Inlogcode='$id'";
$resultaat=mysql_query($sql);
}
我不知道如何将我的查询结果转换回我可以在其余 PHP 脚本中使用的真 bool 值。添加 mysql_fetch_array 似乎是不必要的,但我似乎丢失了 bool 值,因为我总是得到全零或全一。测试数据库当然包含这两个值。
最佳答案
您可以使用单个查询进行更新:
UPDATE personnel SET Present = 1 - Present WHERE Inlogcode=?
考虑使用 PDO 和准备好的语句。您当前的查询容易受到 SQL 注入(inject)攻击
关于php - 使用从 mysql 查询接收到的 bool 值来更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210300/