Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Aktualizowanie bazy danych SQL za pomocą wartości pól wyboru

Dodałem naprawdę zwariowany sposób wykonania aktualizacji. Musisz uważać na wstrzykiwanie SQL i inne złe rzeczy podczas używania post ... upewnij się, że zawsze sprawdzasz i oczyszczasz swoje dane. Sposób "hacky" działa, ponieważ pracujemy z wartościami całkowitymi i te są łatwe do oczyszczenia.

Zrzut ekranu:

To jest moja tabela... Nazwałem ją tak dla 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     |       |
+----+-------+-------+-------+-------+-------+-------+-------+-------+

To jest mój plik PHP.... (wklej go do pustego dokumentu PHP do uruchomienia, jest on samowystarczalny oprócz zmiany połączenia bazy danych i zapytania)

<?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 => $dane) { if (substr($i,0,3)=='row' &&is_numeric(substr($i,3))) { $row_id =substr($i, 3); $dane =array_flip($dane); $wartości =tablica(); for ($x=1; $x<9; $x++) { $wartości[] ="tydzień$x =". ((isset($dane[$x])) ? '1' :'0'); } $stmt ="\nzaktualizuj tak". "\n ustaw ".implode(", \n ",$wartości). "\n gdzie id =$row_id; \n"; $aktualizacja =$mysqli->zapytanie($stmt); if ($update) { print "Wiersz $row_id zaktualizowany pomyślnie.
"; } } print "
"; }}$result =$mysqli->query("wybierz * z tego"); $mysqli->close();?>
fetch_object()) { $liczba =1; print "
\n"; print "Wiersz ".$row->id."
\n"; while($liczba <9) { $tydzień ="tydzień$liczba"; $checkvalue =$wiersz->{$tydzień}; ?> Tydzień checked="checked" />   

\n\n\n"; } ?>

To jest $_POST po przesłaniu:

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
)

Moje „wyświetl źródło” formularza:

<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>

edytuj
Wróciłem do tej odpowiedzi, aby się do czegoś odwołać, i właśnie zdałem sobie sprawę, że ponownie używam ID wartości pól wyboru. W tej sytuacji to nie był problem, ale zawsze jest to zła praktyka. Każde pole wyboru powinno mieć identyfikator, który jest unikalny na stronie. Więc.... tak naprawdę powinno być id="row1week1" , id="row1week2" ... id="row8week1" ... id="row8week8" .




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL - Jak usunąć z tabeli, gdy zagnieżdżony wybór używa tej tabeli?

  2. Jak ustawić ustawienia regionalne dla bieżącego połączenia w MySQL?

  3. Identyfikator zasobu #4 PHP MYSQL

  4. percentyl według COUNT(DISTINCT) ze skorelowanym WHERE działa tylko z widokiem (lub bez DISTINCT)

  5. Jaka jest różnica między MyISAM a InnoDB?