Php update sets everything to 0, but for specific id 1
Basically I have a form with checkboxes of all rows in my database. The user must select the one he wants to see and submit this form. So in the database I want to set the "visible" column for all rows to false, but the one that is selected to true. I thought this should work:
$sql = "UPDATE `questions`
SET `visible` = false;
SET `visible` = true WHERE ID={$radio}" ;
but I apparently cannot run multiple SETs like this. Also, there is no if-else-like statement in php?
What would be a good way to handle this?
+3
source to share
2 answers
You can use conditional statements :
UPDATE `questions`
SET `visible` =
CASE
WHEN ID = '$radio' THEN true
ELSE false
END
Also, make sure you sanitize user input before inserting it into a query.
+3
source to share