我已经更新了我的代码。除非我一次选择更多选择框,否则它可以工作。例如,如果我同时选择 2 个选择框并更新它们,数据库会将它们都更新为 0。当我分别更新它们时,一切正常。
这是现在的代码:
<?php
include "header.php";
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['aan']))
{
$doSave = implode(',',$_POST['aan']);
mysql_query("UPDATE Modules SET
aan = '".mysql_real_escape_string('1')."'
WHERE module_name IN ('".mysql_real_escape_string($doSave)."')") or die (mysql_error());
mysql_query("UPDATE Modules SET
aan = '".mysql_real_escape_string('0')."'
WHERE module_name NOT IN ('".mysql_real_escape_string($doSave)."')") or die (mysql_error());
}
elseif(!isset($_POST['aan']))
{
mysql_query("UPDATE Modules SET
aan = '".mysql_real_escape_string('0')."'
WHERE module_name NOT IN ('".mysql_real_escape_string($doSave)."')") or die (mysql_error());
}
echo $doSave;
}
echo "<h1>Module beheer</h1>";
$getmodules = mysql_query("SELECT * FROM Modules") or die(mysql_error());
echo "<form id='module' method='post'>";
while($modules = mysql_fetch_array($getmodules))
{
echo "<label>".$modules['module_name']."</label>";
echo "<input type='hidden' name='module_name' value='".$modules['module_name']."' />";
if($modules['aan'] == 1)
{
$set_checked = "CHECKED";
}
else
{
$set_checked = "";
}
echo "<input type='checkbox' name='aan[]' value=".$modules['module_name']." ".$set_checked." /><br />";
}
echo "<input type='submit' id='modulesubmit' name='modulesubmit' value='Opslaan'>";
echo "</form>";
include "footer.php";
?>