vendredi 13 novembre 2020

How to save multiple checkbox array from $_POST in variable

I am new to PHP and am looking for some help with the following: I need to implement array for multiple checkboxes in this php code and set data in MySql Database but is not working

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'])) {
              
        $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']);
            
$active = isset($_POST['active']) ? $_POST['active'] : array();
foreach($active as $actives) {
  echo $actives . '<br/>';';  
}

            if ($stmt->execute()) {
                header("location: ../lakota/index.php");
                exit();
            } else {
                echo "Errore! Riprova più tardi.";
            }
            $stmt->close();
        }
    }

    $link->close();
}
?>

html code

<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
                    <div class="form-group">                       
                        <label><b>Stati Attivi</b> &#8211; <i>Selezionare una o più voci interessate.</i></label>
                        <input type="checkbox" name="active[]" value="si"/> si
                        <input type="checkbox" name="active[]" value="no"/> no
                    </div>
                    <input type="submit" class="btn btn-primary" value="Aggiungi">
                    <a href="../lakota/index.php" class="btn btn-default">Annulla</a>
                </form>



Aucun commentaire:

Enregistrer un commentaire