mardi 24 janvier 2023

Update Mysql DB from checkboxes

I am trying to give the values ​​0 or 1 to the fields of a database depending on whether the checkbox is checked or not,

but i can't find it and i can't find where is my problem, can you help me?

<?php

//connexion à la base de donnée
include_once "../connexion.php";
 //on récupère le id dans le lien
$id = $_GET['id'];
//requête pour afficher les infos d'un employé
$req = mysqli_query($con , "SELECT * FROM lecteur_badge_statique WHERE id = $id");
$row = mysqli_fetch_assoc($req);


//vérifier que le bouton modifier a bien été cliqué
if(isset($_POST['button'])){
   //extraction des informations envoyé dans des variables par la methode POST
   extract($_POST);
   //verifier que tous les champs ont été remplis
   if(isset($lb1) && $lb2){
       //requête de modification
       $req = mysqli_query($con, "UPDATE lecteur_badge_statique SET lb1 = '$lb1' , lb2 = '$lb2' , lb3 = '$lb3, lb4 = '$lb4, lb5 = '$lb5, lb6 = '$lb6, lb7 = '$lb7, lb8 = '$lb8, lb9 = '$lb9, lb10= '$lb10'
                                                                    lb11 = '$lb11' , lb12 = '$lb12' , lb13 = '$lb13, lb14 = '$lb14, lb15 = '$lb15, lb16 = '$lb16, lb17 = '$lb17, lb18 = '$lb18, lb19 = '$lb19, lb20= '$lb20'
                                                                    lb21 = '$lb21' , lb22 = '$lb22' , lb23 = '$lb23, lb24 = '$lb24, lb25 = '$lb25, lb26 = '$lb26, lb27 = '$lb27, lb28 = '$lb28, lb29 = '$lb29, lb30= '$lb30'
                                                                    lb31 = '$lb31' , lb32 = '$lb32' , lb33 = '$lb33, lb34 = '$lb34, lb35 = '$lb35, lb36 = '$lb36, lb37 = '$lb37, lb38 = '$lb38, lb39 = '$lb39, lb40= '$lb40'
                                                                    lb41 = '$lb41' , lb42 = '$lb42' , lb43 = '$lb43, lb44 = '$lb44, lb45 = '$lb45, lb46 = '$lb46, lb47 = '$lb47, lb48 = '$lb48, lb49 = '$lb49, lb50= '$lb50'
                                    WHERE id = $id");
        $error_message = mysqli_error($con);
        if($error_message == ""){
            echo "No error related to SQL query.";
        }else{
            echo "Query Failed: ".$error_message;
        }
               
       if($req){//si la requête a été effectuée avec succès , on fait une redirection
            header("location: index.php");
        }else {//si non
            $message = "Employé non modifié";
        }

   }else {
       //si non
       $message = "Veuillez remplir tous les champs !";
   }
}

?>
<div class="form">
        <a href="index.php" class="back_btn"><img src="images/back.png"> Retour</a>
        <h2>Modifier l'employé : <?=$row['nom']?> </h2>
        <p class="erreur_message">
           <?php 
              if(isset($message)){
                  echo $message ;
              }
           ?>
        </p>
        <form action="" method="POST">
            <label>lb1</label>
            <input type="checkbox" name="lb1" value="<?=$row['lb1']?>">
            <label>lb2</label>
            <input type="checkbox" name="lb2" value="<?=$row['lb2']?>">
            <label>lb3</label>
            <input type="checkbox" name="lb3" value="<?=$row['lb3']?>">
            <label>lb4</label>
            <input type="checkbox" name="lb4" value="<?=$row['lb4']?>">
            <label>lb5</label>
            <input type="checkbox" name="lb5" value="<?=$row['lb5']?>">
            <label>lb6</label>
            <input type="checkbox" name="lb6" value="<?=$row['lb6']?>">
            <label>lb7</label>
            <input type="checkbox" name="lb7" value="<?=$row['lb7']?>">
            <label>lb8</label>
            <input type="checkbox" name="lb8" value="<?=$row['lb8']?>">
            <label>lb9</label>
            <input type="checkbox" name="lb9" value="<?=$row['lb9']?>">
            <label>lb10</label>
            <input type="checkbox" name="lb10" value="<?=$row['lb10']?>">
            <label>lb11</label>
            <input type="checkbox" name="lb11" value="<?=$row['lb11']?>">
            <label>lb12</label>
            <input type="checkbox" name="lb12" value="<?=$row['lb12']?>">
            <label>lb13</label>
            <input type="checkbox" name="lb13" value="<?=$row['lb13']?>">
            <label>lb14</label>
            <input type="checkbox" name="lb14" value="<?=$row['lb14']?>">
            <label>lb15</label>
            <input type="checkbox" name="lb15" value="<?=$row['lb15']?>">
            <label>lb16</label>
            <input type="checkbox" name="lb16" value="<?=$row['lb16']?>">
            <label>lb17</label>
            <input type="checkbox" name="lb17" value="<?=$row['lb17']?>">
            <label>lb18</label>
            <input type="checkbox" name="lb18" value="<?=$row['lb18']?>">
            <label>lb19</label>
            <input type="checkbox" name="lb19" value="<?=$row['lb19']?>">
            <label>lb20</label>
            <input type="checkbox" name="lb20" value="<?=$row['lb20']?>">
            <label>lb21</label>
            <input type="checkbox" name="lb21" value="<?=$row['lb21']?>">
            <label>lb22</label>
            <input type="checkbox" name="lb22" value="<?=$row['lb22']?>">
            <label>lb23</label>
            <input type="checkbox" name="lb23" value="<?=$row['lb23']?>">
            <label>lb24</label>
            <input type="checkbox" name="lb24" value="<?=$row['lb24']?>">
            <label>lb25</label>
            <input type="checkbox" name="lb25" value="<?=$row['lb25']?>">
            <label>lb26</label>
            <input type="checkbox" name="lb26" value="<?=$row['lb26']?>">
            <label>lb27</label>
            <input type="checkbox" name="lb27" value="<?=$row['lb27']?>">
            <label>lb28</label>
            <input type="checkbox" name="lb28" value="<?=$row['lb28']?>">
            <label>lb29</label>
            <input type="checkbox" name="lb29" value="<?=$row['lb29']?>">
            <label>lb30</label>
            <input type="checkbox" name="lb30" value="<?=$row['lb30']?>">
            <label>lb31</label>
            <input type="checkbox" name="lb31" value="<?=$row['lb31']?>">
            <label>lb32</label>
            <input type="checkbox" name="lb32" value="<?=$row['lb32']?>">
            <label>lb33</label>
            <input type="checkbox" name="lb33" value="<?=$row['lb33']?>">
            <label>lb34</label>
            <input type="checkbox" name="lb34" value="<?=$row['lb34']?>">
            <label>lb35</label>
            <input type="checkbox" name="lb35" value="<?=$row['lb35']?>">
            <label>lb36</label>
            <input type="checkbox" name="lb36" value="<?=$row['lb36']?>">
            <label>lb37</label>
            <input type="checkbox" name="lb37" value="<?=$row['lb37']?>">
            <label>lb38</label>
            <input type="checkbox" name="lb38" value="<?=$row['lb38']?>">
            <label>lb39</label>
            <input type="checkbox" name="lb39" value="<?=$row['lb39']?>">
            <label>lb40</label>
            <input type="checkbox" name="lb40" value="<?=$row['lb40']?>">
            <label>lb41</label>
            <input type="checkbox" name="lb41" value="<?=$row['lb41']?>">
            <label>lb42</label>
            <input type="checkbox" name="lb42" value="<?=$row['lb42']?>">
            <label>lb43</label>
            <input type="checkbox" name="lb43" value="<?=$row['lb43']?>">
            <label>lb44</label>
            <input type="checkbox" name="lb44" value="<?=$row['lb44']?>">
            <label>lb45</label>
            <input type="checkbox" name="lb45" value="<?=$row['lb45']?>">
            <label>lb46</label>
            <input type="checkbox" name="lb46" value="<?=$row['lb46']?>">
            <label>lb47</label>
            <input type="checkbox" name="lb47" value="<?=$row['lb47']?>">
            <label>lb48</label>
            <input type="checkbox" name="lb48" value="<?=$row['lb48']?>">
            <label>lb49</label>
            <input type="checkbox" name="lb49" value="<?=$row['lb49']?>">
            <label>lb50</label>
            <input type="checkbox" name="lb50" value="<?=$row['lb50']?>">

            <input type="submit" value="Modifier" name="button">
        </form>
    </div>

I would like that when the page loads, the database fields that contain the value 1 are checked and then we can check/uncheck the boxes that we want to update to 1 or 0 in the DB.

But already when loading the page the boxes are not checked when they are 1 in the DB.

value="<?=$row['']?> with checkbox doesn't work the same as input type text?

Thanks in advance for your help :)




Aucun commentaire:

Enregistrer un commentaire