[英]insert multiple rows with specific calculated values
I need to insert into a table "x" number of rows based on how many "groups" and "players" a user enters into a form (that part works) but also i need to label them with specific values and cannot have player value #4. 我需要根据用户输入表单(该部分有效)的“组”和“玩家”数量,在表中插入“ x”行数,但是我还需要用特定值标记它们并且不能具有玩家值#4。
Example: if a user says they need 2 groups and 6 players. 示例:如果用户说他们需要2个小组和6个玩家。 it means there are 2 groups of 6 players each so i will need to insert 12 records into a table and it should look something like this: 这意味着每组有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
here's my form.. 这是我的表格。
<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>
and here's the code to insert records. 这是插入记录的代码。 Right now, it inserts 12 records but can't figure out how to do values of "groups" and "players" 现在,它插入了12条记录,但无法弄清楚“组”和“玩家”的值
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();
}
any help would be great. 任何帮助都会很棒。 thanks. 谢谢。
------- UPDATED VERSION ------- - - - - 更新后的版本 - - - -
inserts rows with correct values (skips value # 4 from players column). 插入具有正确值的行(从玩家列中跳过值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(); }
You would need to push your bind into a double loop: 您需要将绑定推入双循环:
$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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.