samedi 14 novembre 2020

Issue to save values in database with multiple checkboxs in CRUD

I tried to insert multiple checkboxs for "$_POST active" in CRUD but in the database will be saved as "Array" everytime.
I used this code but where exactly I need to put it? foreach($_POST['active'] as $act){//query? }
This is php code

<?php
require_once "../lakota/config.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (isset($_POST['faction'], $_POST['stations'], $_POST['active'], $_POST['pending'], $_POST['influence'], $_POST['id_fact'])) {
    
    if(isset($_POST["submit"])){
    $activearr=$_POST["active"];
    $newvalues=  implode(",", $activearr);
    include_once "../lakota/checkboxClass.php";
    $checkBoxClass=new checkboxClass();
    echo $checkBoxClass->addtoDatabase($newvalues);
}
    
        $sql = "INSERT INTO lakotabgs (faction, stations, active, pending, influence, id_fact) VALUES (?,?,?,?,?,?)";
        
        if ($stmt = $link->prepare($sql)) {
            $stmt->bind_param("ssssss", $_POST['faction'], $_POST['stations'], $_POST['active'], $_POST['pending'], $_POST['influence'], $_POST['id_fact']);
            
            if ($stmt->execute()) {
                header("location: ../lakota/index.php");
                exit();
            } else {
                echo "Errore! Riprova più tardi.";
            }
            $stmt->close();
        }
    }

    $link->close();
}
?>

This is HTML code

<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
                    <div class="form-group">
                        <input name="active[]" class="form-control" id="example1" type="checkbox" value="Nessuno"  />
                        <label for="example1">Nessuno</label>
                        <input name="active[]" class="form-control" id="example2" type="checkbox" value="Tutti quanti"  />
                        <label for="example2">Tutti quanti</label>
                    </div>
                    <input type="submit" id="submit" name="submit" class="btn btn-primary" value="Aggiungi">
                </form>



Aucun commentaire:

Enregistrer un commentaire