在复选框上添加项目到数组

时间:2013-08-02 17:25:46

标签: php mysql

首先,我知道很多这个在新的php中被删除了,一旦我得到最后几件事,我将使用新命令更新我的整个网站。

如果选中该复选框,我想要做的是将复选框(播放器)的值添加到数组中。

然后,当我在rosterverify.php上,在mysql查询中使用INSERT时,为数组上的每个项创建新行。

我已经检查了w3schools,并进行了一些谷歌搜索,但是空了。

<form name="addplayers" action='rosterverify.php' method="post">
    <table class="bordered">
        <thead>
            <tr>
                <th>Player Name</th>
                <th>Advanced Class</th>
                <th>Designation</th>
                <th></th>
            </tr>
        </thead>
        <?php
        $sql = "SELECT * FROM reguserstest WHERE `server`='$server' AND `guild`='$guild'";
        $result = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
        $i=1;                   
        while (($row = mysql_fetch_assoc($result))) {
            if (is_int($i/2)) {
                echo "<tr><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players' value='". $row['toonname'] . "'></td></tr>";
            } else {
                echo "<tr class='alt'><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players' value='". $row['toonname'] . "'></td></tr>";
            }
            $i++;
        }

        mysql_close($con);
        ?>                       
    </table>
    <br />
    <center><input type="submit" name="submit" value="Add Players to Team" class="ebutton" /></center>
</form>

这是rosterverify.php我还没有执行这段代码,因为我不想搞砸我的目标。

$players=$_POST['players'];
$arrlength=count($players);

for($x=0;$x<$arrlength;$x++)
{
    $sql="INSERT INTO rated_teams (players) VALUES ('$players[$x]')";

        mysql_query($sql,$con);
        if (mysql_errno()) {
            echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br />\n$sql\n<br />" . $player[$x] . " was not added to the table.";
        }
}

2 个答案:

答案 0 :(得分:5)

players[]

等复选框设置名称
<input type='checkbox' name='players[]' value='some_value'>

提交后,您将拥有$_POST['players'],其中包含选定的值。

答案 1 :(得分:0)

use this code
<form name="addplayers" action='rosterverify.php' method="post">
    <table class="bordered">
        <thead>
            <tr>
                <th>Player Name</th>
                <th>Advanced Class</th>
                <th>Designation</th>
                <th></th>
            </tr>
        </thead>
        <?php
        $sql = "SELECT * FROM reguserstest WHERE `server`='$server' AND `guild`='$guild'";
        $result = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
        $i=1;                   
       while (($row = mysql_fetch_assoc($result))) {
            if (is_int($i/2)) {
                echo "<tr><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players[]' value='". $row['toonname'] . "'></td></tr>";
            } else {
                echo "<tr class='alt'><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players[]' value='". $row['toonname'] . "'></td></tr>";
            }
            $i++;
        }


        mysql_close($con);
        ?>                       
    </table>
    <br />
    <center><input type="submit" name="submit" value="Add Players to Team" class="ebutton" /></center>
</form>

only changes with name='players' replace with name='players[]'