Mysql
 sql >> Base de données >  >> RDS >> Mysql

Mise à jour de la base de données SQL avec des valeurs de case à cocher

J'ai ajouté une façon vraiment hacky de faire la mise à jour. Vous devez vous méfier de l'injection SQL et d'autres mauvaises choses lors de l'utilisation de post ... assurez-vous de toujours valider et assainir vos données. La méthode "hacky" fonctionne car nous travaillons avec des valeurs entières et celles-ci sont facilement nettoyées.

Capture d'écran :

Ceci est ma table... Je l'ai appelée ainsi pour stackoverflow...

+----+-------+-------+-------+-------+-------+-------+-------+-------+
| id | week1 | week2 | week3 | week4 | week5 | week6 | week7 | week8 |
+----+-------+-------+-------+-------+-------+-------+-------+-------+
|  1 |       |       |       |       |       |       |       |       |
|  2 | 1     |       |       |       |       |       |       |       |
|  3 | 1     |       | 1     |       |       |       |       |       |
|  4 | 1     |       | 1     |       | 1     |       |       |       |
|  5 | 1     |       | 1     |       | 1     |       | 1     |       |
+----+-------+-------+-------+-------+-------+-------+-------+-------+

Ceci est mon fichier PHP.... (collez-le dans un document PHP vide pour l'exécuter, il est autonome en plus de changer la connexion à la base de données et la requête)

<?php 
$mysqli = new mysqli("localhost", "root", "password", "test"); 

if (!empty($_POST)) {
   print "<pre>POST VARS: \n".print_r($_POST,true)."</pre>";

   foreach($_POST as $i => $data) 
   { 
      if (substr($i,0,3)=='row' && is_numeric(substr($i,3)))
      {
         $row_id = substr($i,3);  
         $data = array_flip($data); 
         $values = array(); 

         for ($x=1; $x<9; $x++) {
            $values[] = "week$x = ". ((isset($data[$x])) ? '1' : '0');  
         }   

         $stmt = "\nupdate so ".
                 "\n  set ".implode(", \n      ",$values).
                 "\n  where id = $row_id; \n"; 

         $update = $mysqli->query($stmt); 
         if ($update) { print "Row $row_id updated successfully.<br/>"; } 
      }
      print "<br/>"; 
   }
}
$result = $mysqli->query("select * from so"); 

$mysqli->close();
?>

<form method="post" id="updating" action="<?php $_PHP_SELF ?>"> 
   <?php
   while($row = $result->fetch_object())
   {

      $count = 1;

      print "<div style='border:1px solid black; display:inline-block;'>\n";
      print "Row ".$row->id."<br/>\n"; 
      while($count < 9) 
      {
         $week = "week$count";

         $checkvalue = $row->{$week};
         ?>
         Week<?php echo $count ?> <input type="checkbox" id="Week<?php echo$count;?>" name="row<?php echo $row->id 
         ?>[]" value="<?php echo $count;?>" <?php if($checkvalue==1){
         ?>  checked="checked" <?php } ?> />&nbsp;&nbsp;&nbsp;
         <?php   
         $count++; 
      }
      print "</div><br/><br/>\n\n\n"; 
   }
   ?>
        <input name="update" type="submit" id="update" value="Update">
    </form>
" ; foreach($_POST as $i => $data) { if (substr($i,0,3)=='row' &&is_numeric(substr($i,3))) { $row_id =substr($i, 3); $data =array_flip($data); $valeurs =tableau(); for ($x=1; $x<9; $x++) { $values[] ="semaine$x =". ((isset($data[$x])) ? '1' :'0'); } $stmt ="\nmettre à jour donc ". "\n set ".implode(", \n ",$values). "\n où id =$row_id ; \n" ; $update =$mysqli->query($stmt); if ($update) { print "La ligne $row_id a été mise à jour avec succès.
" ; } } imprime "
" ; }}$result =$mysqli->query("select * from so"); $mysqli->close();?>
fetch_object()) { $compte =1 ; print "
\n" ; print "Row ".$row->id."
\n" ; while($count <9) { $week ="week$count" ; $checkvalue =$row->{$week} ; ?> Semaine check="coché" />   

\n\n\n" ; } ?>

C'est le $_POST après soumission :

POST VARS: 
Array
(
    [row2] => Array
        (
            [0] => 1
        )

    [row3] => Array
        (
            [0] => 1
            [1] => 3
        )

    [row4] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
        )

    [row5] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
            [3] => 7
        )

    [update] => Update
)

Ma "vue source" du formulaire :

<form method="post" id="updating" action=""> 
   <div style='border:1px solid black; display:inline-block;'>
Row 1<br/>
         Week1 <input type="checkbox" id="Week1" name="row1[]" value="1"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row1[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row1[]" value="3"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row1[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row1[]" value="5"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row1[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row1[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row1[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 2<br/>
         Week1 <input type="checkbox" id="Week1" name="row2[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row2[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row2[]" value="3"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row2[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row2[]" value="5"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row2[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row2[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row2[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 3<br/>
         Week1 <input type="checkbox" id="Week1" name="row3[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row3[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row3[]" value="3"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row3[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row3[]" value="5"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row3[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row3[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row3[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 4<br/>
         Week1 <input type="checkbox" id="Week1" name="row4[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row4[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row4[]" value="3"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row4[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row4[]" value="5"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row4[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row4[]" value="7"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row4[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


<div style='border:1px solid black; display:inline-block;'>
Row 5<br/>
         Week1 <input type="checkbox" id="Week1" name="row5[]" value="1"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week2 <input type="checkbox" id="Week2" name="row5[]" value="2"  />&nbsp;&nbsp;&nbsp;
                  Week3 <input type="checkbox" id="Week3" name="row5[]" value="3"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week4 <input type="checkbox" id="Week4" name="row5[]" value="4"  />&nbsp;&nbsp;&nbsp;
                  Week5 <input type="checkbox" id="Week5" name="row5[]" value="5"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week6 <input type="checkbox" id="Week6" name="row5[]" value="6"  />&nbsp;&nbsp;&nbsp;
                  Week7 <input type="checkbox" id="Week7" name="row5[]" value="7"   checked="checked"  />&nbsp;&nbsp;&nbsp;
                  Week8 <input type="checkbox" id="Week8" name="row5[]" value="8"  />&nbsp;&nbsp;&nbsp;
         </div><br/><br/>


        <input name="update" type="submit" id="update" value="Update">
    </form>

modifier
Je suis revenu à cette réponse pour faire référence à quelque chose et je viens de réaliser que je réutilisais ID valeurs pour les cases à cocher. Ce n'était pas un problème dans cette situation mais c'est toujours une mauvaise pratique. Chaque case à cocher doit avoir un identifiant unique sur la page. Donc... ça devrait vraiment être id="row1week1" , id="row1week2" ... id="row8week1" ... id="row8week8" .