0

首先,我知道在新的 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.";
        }
}
4

2 回答 2

5

为复选框设置名称,例如players[]

<input type='checkbox' name='players[]' value='some_value'>

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

于 2013-08-02T17:31:02.140 回答
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[]'
于 2013-08-02T18:00:47.300 回答