我真的不知道最好的表达方式,所以我会尽力描述我想要实现的目标。我有一个像这样的表格内的表格<form action="manager-match-report-run.php" method="post" autocomplete="off" name="registerForm"> <div class="table-responsive"> <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0"> <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>Goals</th> <th>Assists</th> <th>Match Rating</th> <th hidden>playerId</th> </tr> </thead> <tbody> <?php $sql_players = "SELECT * FROM Player WHERE teamId = '$teamId' AND selected = '1'"; $res_players = mysqli_query($conn, $sql_players); while($row_players = $res_players->fetch_assoc()) { echo "<tr>"; echo "<td>" . $row_players["firstName"]. "</td>"; echo "<td>" . $row_players["lastName"]. "</td>"; echo "<td><div class='form-group'><input name='goals' class='form-control py-4' type='number'/></div></td>"; echo "<td><div class='form-group'><input name='assists' class='form-control py-4' type='number'/></div></td>"; echo "<td><div class='form-group'><input name='matchRating' class='form-control py-4' type='number' step='0.01' min='0' max='10'/></div></td>" ?> <td hidden><input name="playerId" value="<?php echo $row_players['playerId'];?>"></td> <?php echo "</tr>"; }?> </tbody> </table> </div>我正在寻找将数据插入 Player 表中正确行的正确方法。例如,更新球员表,使 Alison 有 5 个进球、1 个助攻、8 个比赛评分,Ben 有 0 个进球、1 个助攻、7 个比赛评分等。我假设我必须使用某种循环,但到目前为止还没有运气,所以任何帮助或建议都会很棒,谢谢:)
1 回答
紫衣仙女
TA贡献1839条经验 获得超15个赞
您的输入都具有相同的名称,因此只有最后一个值会发布到您的应用程序。
为每个控件添加动态名称,例如name='matchRating',使用
$playerName = htmlentities($playername);
$inputname = sprintf("matchRating[%s]", $playerName);
printf("<input name=\"%s\" type=\"text\"/>", $inputname);
提交表单后,您将获取数组中的数据,您可以循环遍历该数组以更新数据库记录:
$matchRatings = $_GET["matchRating"];
foreach ($matchRatings as $playerName => $rating) {
// Update player rating in database
// Important: Do not forget to guard against SQL injection!
$sql = sprintf("update MYTABLE set MATCHRATING=%d where PLAYERNAME='%s'", $rating, $playerName);
}
- 1 回答
- 0 关注
- 71 浏览
添加回答
举报
0/150
提交
取消