在db中插入所选复选框值的总数

时间:2012-12-20 01:33:04

标签: php mysql database checkbox foreach

我是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());
        }




    ?>

2 个答案:

答案 0 :(得分:0)

尝试更改复选框标记,如下所示:

 foreach($perms as $key => $value)
    {
       echo "<input type='checkbox' value='".$value."' name='checkbox[]'>\n";
        echo "<label for='".$key."'>".ucfirst($key)."</label><br>\n";
    }

然后像这样访问它:

  if(isset($_POST['submit']))
    {
        $total = 0; 

        foreach ($_POST['checkbox'] as $checkbox) 
        {
                $total += $checkbox; 

        }


        echo $total;
        mysql_query("INSERT INTO perms (name, rights) VALUES(name, $total)") or die(mysql_error());
    }

答案 1 :(得分:0)

我看到一些问题。有些会比其他问题引起更多问题。

(1)您的复选框中缺少name属性。 (2)您缺少结束</form>标记。 (3)您的查询可能失败,因为您有VALUES(name, $total),我认为name是一个字符串,因此需要引号VALUES('name', $total),或更可能VALUES('$name', $total) < / p>

另外,我不明白为什么要在初始页面加载时连接到数据库。我会把它移到你的isset($_POST['submit'])里面。

试试这个 -

<?php
    $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' name='checkboxes[]' value='".$value."'>\n";  
        echo "<label for='".$key."'>".ucfirst($key)."</label><br>\n";
    }
        echo "<input type='text' name='name'>";
        echo "<input type='submit' name='submit'>";
        echo "</form>";

    if(isset($_POST['submit']))
    {
       mysql_connect( 'localhost',  'root', '')or die("cannot connect");
       mysql_select_db('webgame')or die("cannot select DB");

        $total = 0; 
        $name  = mysql_real_escape_string($_POST['name']); 
        foreach ($_POST['checkboxes'] as $keys=>$vals)
        {
                $total += $vals;
        }

    mysql_query("INSERT INTO perms (name, rights) VALUES('$name', $total)") or die(mysql_error()); 
   }
?>

此外,自PHP 5.5.0起,mysql_*扩展名已弃用,将来将被删除。相反,应使用MySQLiPDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API guiderelated FAQ