在 php 5.6.23 中设置变量值

setting up variable value in php 5.6.23

我正在根据复选框

上的选择更新我的sql查询
        $selected = ($data['selected'] == true) ? 1 : 0;
        echo $data['selected'];
        //prints true when checkbox is checked
        //prints false when unchecked
        echo $sql = "update test_table set selected = " . $selected . " where id = " . $data['product_id'];

但在这两种情况下 $selected 都设置为 1 。 当

$data['selected'] == false it should save $selected = 0 but it does not.

我错过了什么?

假设您将 false 作为隐藏输入传递,将 true 作为复选框传递,在这种情况下,您将得到一个或另一个;它们作为字符串传递:

$selected = ($data['selected'] == 'true') ? 1 : 0;

但我想问,为什么不直接从表单中传递 01

如果您只将复选框作为 true 传递,则:

$selected = isset($data['selected']) ? 1 : 0;