【发布时间】:2012-12-20 11:05:58
【问题描述】:
我对 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());
}
?>
【问题讨论】:
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.
标签: php mysql database checkbox foreach