How to update values to 1 or 0 into database with checkboxes in php?

95
September 18, 2018, at 12:20 PM

What I'm trying to do is show a database column (users) and add a checkbox next to each one.

The checkbox saves the value previously entered (state column): 1 or 0, 1 being checked and 0 being un-checked. I want to be able to show all active and inactive users with their respective checked or unchecked boxes and when I uncheck a checked box turn the value into 0 and when checking an unchecked box turn the value into 1, updating all the changes made at once respectively (user1=1, user2=0 etc) in the database by click an update button.

This is what I have so far. I have been reading questions and code samples but havenĀ“t really understood.

<?php
require_once 'config.php';
$query2 = "SELECT usuario, estado FROM administrador";
$res = mysqli_query($link, $query2);
if(mysqli_num_rows($res)){
    $select= '<form method="post">';
    while($rs=mysqli_fetch_array($res)){
        if($rs['estado'] == 1)
        {
            $estadoActual = "checked";
        }
        else
        {
            $estadoActual = "";
        }
        $select.='<input type="hidden" NAME="estado" " value="0">';
        $select.='<input type="checkbox" NAME="estado" value="1" '.$estadoActual.' class="toggle-switch">'.$rs['usuario'].'<br>';
    }
} else {
    echo "No hay usuarios creados";
}
$select.='</form>';
echo $select;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $estadoAdmin = $_POST['estado'];
    $query3 = "UPDATE administrador SET estado=$estadoAdmin";
    $res2 = mysqli_query($link, $query3);
    header("location: activar_admin.php");
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>prueba</title>
</head>
<body>
    <input type="submit" class="btn btn-primary" value="Submit">   
</body>
</html>
READ ALSO
PHP code is not working after encoded using lockit

PHP code is not working after encoded using lockit

I have set of php filesThese files are working perfectly fine

93
PHP - How to pass a json array properly in a URL string [duplicate]

PHP - How to pass a json array properly in a URL string [duplicate]

This question already has an answer here:

99
Search for SQL records and return the number of rows found with SQLSRV_SCROLL_NEXT

Search for SQL records and return the number of rows found with SQLSRV_SCROLL_NEXT

Apologies if this has been posted but I've spent two days searching and found nothing

137
How to track login activity of user in Laravel 5.6

How to track login activity of user in Laravel 5.6

How to track login activity of user in Laravel 56

146