I am updating my sql query based on the selection made on checkbox
$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'];
But in both cases $selected is set to 1 . When
$data['selected'] == false it should save $selected = 0 but it does not.
What am I missing?
Assuming that you are passing false
as a hidden input and true
for the checkbox, in which case you will get one or the other; they are passed as strings:
$selected = ($data['selected'] == 'true') ? 1 : 0;
But I would ask, why not just pass 0
and 1
from the form?
If you are only passing the checkbox as true
, then:
$selected = isset($data['selected']) ? 1 : 0;