使用php从数据库中显示多个复选框值

时间:2016-08-16 15:50:30

标签: php html mysql forms

我需要显示多个复选框,但我的代码只显示最后一次检查。

为什么会这样?

下面是我的代码:

<html>
<head>
    <title>CHECK</title>
</head>
<body>
    <?php
    ini_set("display_errors","off");
    if(isset($_POST['vil']))
    {
        $t=$_POST['vil'];
    }
    Else
    {
        $t="";
    }
    ?>

    <form method="post" id="monform" action="">
        <table border=1 width=800  cellpadding=0 cellspacing=1>
            <tr align=center>
                <td>
                    Case
                </td>
                <td>
                    Code
                </td>
                <td>
                    Nom
                </td>
            </tr>
            <?php

            ini_set("display_errors","off");

            $con = @mysql_connect("localhost","root","");
            if(!$con)
            {
                echo "erreur de connexion";
            }
            Else
            {
                mysql_select_db("database",$con);

                $sql = "SELECT * FROM table";
                $result = mysql_query($sql);

                while ($tab=mysql_fetch_array($result))
                {
                    $a=$tab["code"];
                    $b=$tab["nom"];

                    echo "                                       "
                    <tr align=center>
                    <td>
                    <input type=checkbox name=vil[] value=$a>$a<br>
                    </td>
                    <td>
                    $a
                    </td>
                    <td>
                    $b
                    </td>
                    </tr>
                    ";
                }

                ?>
                </table>

                <br><br><input type="submit" name="valider" value="valider">


                <table border=1 width=800  cellpadding=0 cellspacing=1>
                <br><br><?php
                foreach ($t as $variablename)
                {
                    $sql = "SELECT * FROM table WHERE code='".$variablename."'";
                    $result = mysql_query($sql);
                }
                while ($tab=mysql_fetch_array($result))
                {
                    $a=$tab["code"];
                    $b=$tab["nom"];
                    {
                        for ($i=0;$i<count($t);$i++)
                        {
                            echo $a."__".$b."</br>" ;

                            if(isset($t))
                            {
                                echo "                               "
                                <td>
                                $a
                                </td>
                                <td>
                                $b
                                </td>
                                ";
                            }
                        }
                    }
                }
            }


            ?>
        </table>
    </form>
</body>
</html>

0 个答案:

没有答案