Saya menambahkan beberapa cara yang benar-benar hacky untuk melakukan pembaruan. Anda perlu berhati-hati terhadap SQL Injection dan hal-hal buruk lainnya saat menggunakan post
... pastikan Anda selalu memvalidasi dan membersihkan data Anda. Cara "retas" berfungsi karena kami bekerja dengan nilai integer dan mudah dibersihkan.
Tangkapan layar:
Ini meja saya... Saya menyebutnya demikian untuk 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 | |
+----+-------+-------+-------+-------+-------+-------+-------+-------+
Ini adalah file PHP saya.... (tempelkan di dokumen PHP kosong untuk dijalankan, itu mandiri selain mengubah koneksi db dan kueri)
<?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 } ?> />
<?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); $nilai =array(); for ($x=1; $x<9; $x++) { $nilai[] ="minggu$x =". ((isset($data[$x])) ? '1' :'0'); } $stmt ="\nperbarui jadi ". "\n set ".implode(", \n ",$nilai). "\n di mana id =$row_id; \n"; $perbarui =$mysqli->query($stmt); if ($update) { print "Baris $row_id berhasil diperbarui."; } } print "
"; }}$result =$mysqli->query("pilih * dari sana"); $mysqli->close();?>