0

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




    ?>
4

2 回答 2

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());
    }
于 2012-12-20T01:52:16.880 回答
0

我看到几个问题。有些会导致比其他更多的问题。

(1) 您缺少name复选框中的属性。(2) 您缺少结束</form>标签。(3) 您的 Query 可能像您一样失败VALUES(name, $total),我假设name它是一个字符串,所以它需要用引号括起来VALUES('name', $total),或者更有可能VALUES('$name', $total)

另外,我不明白您为什么在初始页面加载时连接到数据库。我会把它移到你的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()); 
   }
?>

此外,该mysql_*扩展自 PHP 5.5.0 起已弃用,将来将被删除。相反,应该使用MySQLiPDO_MySQL扩展。另请参阅MySQL:选择 API 指南相关的常见问题解答以获取更多信息。

于 2012-12-20T02:53:02.937 回答