sql >> Databasteknik >  >> RDS >> Mysql

Uppdatera SQL-databas med kryssrutavärden

Jag lade till ett riktigt hackigt sätt att göra uppdateringen. Du måste akta dig för SQL Injection och andra dåliga saker när du använder post ... se till att du alltid validerar och sanerar dina data. Det "hackiga" sättet fungerar eftersom vi arbetar med heltalsvärden och de är lätta att rengöra.

Skärmdump:

Det här är mitt bord... Jag kallade det så för 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     |       |
+----+-------+-------+-------+-------+-------+-------+-------+-------+

Det här är min PHP-fil.... (lägg den i ett tomt PHP-dokument för att köra, den är fristående förutom att ändra db-anslutningen och frågan)

<?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 som $i => $data) { if (substr($i,0,3)=='row' &&is_numeric(substr($i,3))) { $row_id =substr($i, 3); $data =array_flip($data); $värden =array(); för ($x=1; $x<9; $x++) { $values[] ="vecka$x =". ((isset($data[$x])) ? '1' :'0'); } $stmt ="\nuppdatera så ". "\n ställ in ".implode(", \n ",$värden). "\n där id =$rad_id; \n"; $update =$mysqli->query($stmt); if ($update) { print "Rad $row_id uppdaterad framgångsrikt.
"; } } skriv ut "
"; }}$result =$mysqli->query("välj * från så"); $mysqli->close();?>
fetch_object()) { $count =1; print "
\n"; skriv ut "Rad ".$row->id."
\n"; while($count <9) { $week ="week$count"; $checkvalue =$row->{$week}; ?> Vecka checked="checked" />   

\n\n\n"; } ?>

Det här är $_POST efter inlämning:

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
)

Min "visa källa" för formuläret:

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

redigera
Jag kom tillbaka till det här svaret för att referera till något och jag insåg precis att jag återanvände ID värden för kryssrutorna. Det var inte ett problem i den här situationen men det är alltid dålig praxis. Varje kryssruta bör ha ett ID som är unikt på sidan. Så... det borde verkligen vara id="row1week1" , id="row1week2" ... id="row8week1" ... id="row8week8" .




  1. Hur konverterar man 1985-02-07T00:00:00.000Z (ISO8601) till ett datumvärde i Oracle?

  2. MySQL Rekursiv få alla barn från förälder

  3. Hitta dubbletter i samma tabell i MySQL

  4. Indexera en MySql TEXT kolumn?