gpt4 book ai didi

php - 插入具有特定计算值的多行

转载 作者:行者123 更新时间:2023-11-29 00:13:18 25 4
gpt4 key购买 nike

我需要根据用户在表单中输入的“组”和“玩家”数量(该部分有效)在表中插入“x”行数,但我还需要用特定值标记它们并且不能玩家值(value) #4。

例子:如果用户说他们需要 2 个组和 6 个玩家。这意味着有 2 组,每组 6 名玩家,所以我需要在表中插入 12 条记录,它应该看起来像这样:

id     group     player     
1 1 1
2 1 2
3 1 3
4 1 5
5 1 6
6 1 7
7 2 1
8 2 2
9 2 3
10 2 5
11 2 6
12 2 7

这是我的表格..

<form id="setup" name="setup" method="post" action="bin/setup.php">
<input type="text" name="groups" placeholder="# OF GROUPS" value="">
<input type="text" name="players" placeholder="# OF PLAYERS" value="">
<button class="btn" name="submit" type="submit">Insert</button>
</form>

这是插入记录的代码。现在,它插入了 12 条记录,但不知道如何处理“groups”和“players”的值

if(isset($_POST['submit'])) {
$group = ( ($_POST['groups']) * ($_POST['players']) );

$sql = "INSERT INTO table (groups, players)
VALUES (:groups, :players)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':groups', $_POST['groups']);
$stmt->bindParam(':players', $_POST['players']);

for ($i = 0; $i < $tables; $i++) {
$stmt->execute();
}

header("Location:/");
exit;

} else {
header("Location:/?msg=error");
exit();
}

任何帮助都会很棒。谢谢。

--------更新版本--------

  • 插入具有正确值的行(跳过玩家列中的值 #4)。

        $sql = "INSERT INTO table (groups, players) 
    VALUES (:groups, :players)";
    $stmt = $db->prepare($sql);

    for($i=1; $i <= $_POST['groups']; $i++)
    for($j=1; $j <= $_POST['players']+1; $j++)
    {
    if($j == 4){continue;}
    $stmt->bindParam(':groups', $i);
    $stmt->bindParam(':players', $j);
    $stmt->execute();
    }

最佳答案

您需要将绑定(bind)插入双循环:

$sql = "INSERT INTO table (groups, players) VALUES (:groups, :players)";
for($i=1; $i <= $_POST['groups']; $i++)
for($j=1; $j <= $_POST['players']; $j++)
{
$stmt->bindParam(':groups', $i);
$stmt->bindParam(':players', $j);
$stmt->execute();
}

关于php - 插入具有特定计算值的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23893992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com