我真的不知道最好的表達方式,所以我會盡力描述我想要實現的目標。我有一個像這樣的表格內的表格<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 關注
- 100 瀏覽
添加回答
舉報
0/150
提交
取消