Update multiple checkbox value in php

This question has probably been asked multiple times but I still cannot figure out a proper working solution to my problem. So, I have an sql table (id-> int and status-> tiny-int) which needs to be activated without submit button and retain their current state either on or off. I found the solution to my problem here. But, this is only for one checkbox and I need it to be for four checkboxes. My attempt at it sorta works but I feel there is probably a better way of doing it. Can someone please provide me with any suggestions?

thanks

>
if (isset($_POST['checkbox1'])) { $sql1="UPDATE switch SET status = '1' WHERE id = '1'"; } else { $sql1="UPDATE switch SET status = '0' WHERE id = '1'"; } $result=$conn->query($sql1); if (isset($_POST['checkbox2'])) { $sql2="UPDATE switch SET status = '1' WHERE id = '2'"; } else { $sql2="UPDATE switch SET status = '0' WHERE id = '2'"; } $result=$conn->query($sql2);

asked Dec 26, 2021 at 11:36

Update multiple checkbox value in php

1

You only need to multiply your code blocks following @Kudzė 's instructions. Like this:

+Tip: at the future when you want to store inputs from the users in the database please use prepared SQL statements this is prevent SQL injection.

 0){
    if (isset($_POST['checkbox1'])) {
        $sql1="UPDATE switch SET status = 1 WHERE id = 1";
    } else {
        $sql1="UPDATE switch SET status = 0 WHERE id = 1";
    }
    $result=$conn->query($sql1);

    if (isset($_POST['checkbox2'])) {
        $sql2="UPDATE switch SET status = 1 WHERE id = 2";
    } else {
        $sql2="UPDATE switch SET status = 0 WHERE id = 2";
    }
    $result=$conn->query($sql2);

    if (isset($_POST['checkbox3'])) {
        $sql3="UPDATE switch SET status = 1 WHERE id = 3";
    } else {
        $sql3="UPDATE switch SET status = 0 WHERE id = 3";
    }
    $result=$conn->query($sql3);

    if (isset($_POST['checkbox4'])) {
        $sql4="UPDATE switch SET status = 1 WHERE id = 4";
    } else {
        $sql4="UPDATE switch SET status = 0 WHERE id = 4";
    }
    $result=$conn->query($sql4);
}

?>



    
    
    
    Document


    

answered Dec 26, 2021 at 12:50

Update multiple checkbox value in php

adampwebadampweb

1,0741 gold badge8 silver badges17 bronze badges

1

How can I add multiple checkbox values in database using PHP?

Insert Multiple Checkbox Value in Database Using PHP.
Create an HTML form, test_post. php, with multiple checkboxes as shown below. ... .
Select multiple checkboxes as shown below..
Now click on the submit button and a popup will be shown for confirmation as shown below. Output. Insert checkbox value in database..

How to update multiple records in PHP?

Steps in PHP Multiple Rows Update/Delete.
Select rows using checkbox input..
Show form UI for updating table columns..
Submit array of row details to PHP..
Iterate through row details array to apply update/delete query for each..

How to update multiple select option in PHP?

It makes easier to update records instead of updating one by one. With checkboxes, enable multiple records selected from the list..
Table structure. Create users table. ... .
Configuration. Create a config. ... .
Record List. ... .
Update Records. ... .
Output. ... .
Conclusion..

How do you checked multiple checkboxes in PHP?

To get all the values from the checked checkboxes, you need to add the square brackets ( [] ) after the name of the checkboxes. When PHP sees the square brackets ( [] ) in the field name, it'll create an associative array of values where the key is the checkbox's name and the values are the selected values.