我对 PHP 很陌生,并且一直在尝试将复选框值的总数存储在数据库中,但出于某种原因(可能缺乏逻辑)。它不起作用:
<?php
mysql_connect( 'localhost', 'root', '')or die("cannot connect");
mysql_select_db('webgame')or die("cannot select DB");
$perms = array(
'writePost' => 1,
'readPost' => 2,
'deletePost' => 4,
'addUser' => 8,
'deleteUser' => 16,
);
echo "<form method='post' action='v3.php'>";
foreach($perms as $key => $value)
{
echo "<input type='checkbox' value='".$value."'>\n";
echo "<label for='".$key."'>".ucfirst($key)."</label><br>\n";
}
echo "<input type='text' name='name'>";
echo "<input type='submit' name='submit'>";
if(isset($_POST['submit']))
{
$total = 0;
foreach ($_POST as $key => $value)
{
if( isset($perms[ $value ]) )
$total += $perms[ $value ];
}
mysql_query("INSERT INTO perms (name, rights) VALUES(name, $total)") or die(mysql_error());
}
?>