php - 将项目添加到复选框上的数组

标签 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.";
        }
}

最佳答案

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

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

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

关于php - 将项目添加到复选框上的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18022773/

相关文章:

PHP JWT token 无效签名

php - 我想在单击删除按钮后获取 id,但无法在其他页面 php 中发送其值

javascript - 如何将 id IS NOT NULL 与转义变量 ??/?

php - 在 MySQL 中使用声明

java - 无法在Android代码中的Alarm OnReceive方法上执行select.php

php - Swift - 将字典发送到服务器时格式错误的数据

mysql - 如何对非联合兼容表进行 UNION

java - Spring Boot JDBC-Test DB Connection Leak 运行所有测试时

java - Spring Data JPA - 编码 UTF-8 不起作用

php - 为什么 PHP PDO 在使用持久连接时连接到不同的数据库?