在我的 HTML 代码中,我有一个表格,但最初没有加载任何数据行,只有一个标题和列标签。单击按钮后,将根据 SQL 语句中从数据库中选择的行数将许多行加载到此表中。在这种情况下,如果我的球队在数据库中有七名注册球员,我的表格将显示七行(通过 PHP 回显语句)。在每一行中,我都有一个带有玩家姓名和文本输入框的复选框。
这是在单击结果后按钮后添加行的 jQuery :
$("#post-result").click(function(){
$("#post-result").hide();
$("#confirm-result").show();
$("#cancel").show();
$("#home-player-count").show();
$("#home-score").html("<input class='form-input f-score' id='home-score-input' type=number name=home_score maxlength=2>");
$("#away-score").html("<input class='form-input f-score' id='away-score-input' type=number name=away_score maxlength=2>");
$("#stats-home").after(<?php
echo "\"";
$i = 0;
$sql2 = mysqli_query($link, "SELECT * FROM user_accounts WHERE ps4_club= '" . $myclub . "'");
while ($row = mysqli_fetch_array($sql2, MYSQLI_ASSOC))
{
$i++;
echo "<tr id='player-".$i."'><td class='match-stats-name'><input type='checkbox' class='f-player' checked='true' value='" . $row['username'] . "' name='home_player_".$i."'>". $row['username'] ."";
echo "<td><input class='form-input f-stats single' maxlength=1 name='home_player_".$i."_rating_1'> . <input class='form-input f-stats single' maxlength=1 name='home_player_".$i."_rating_2'></td>";
echo "<td><input class='form-input f-stats single' maxlength=1 name='home_player_".$i."_goals'></td>";
echo "<td><input class='form-input f-stats single' maxlength=1 name='home_player_".$i."_assists'></td>";
echo "<td><input class='form-input f-stats' maxlength=2 name='home_player_".$i."_tackles_won'> / <input class='form-input f-stats' maxlength=2 name='home_player_".$i."_tackles_made'></td></tr>";
}
echo "\"";
?>)
<?php
$_SESSION["match_id"] = $id;
$_SESSION["player_count"] = $i;
$_SESSION["team"] = $team;
?>
$("#stats-home").after是添加行的位置。
我遇到的问题是当我尝试将表中的这些数据输入到我的数据库中时。这是我单击表单的确认结果按钮后加载的部分 PHP 代码:
<?php
include "../config.php";
if (isset($_POST["confirm_result"]))
{
session_start();
for ($i = 1; $i <= $_SESSION["player_count"]; $i++)
{
$sql = $link->prepare("INSERT INTO ps4_apl_1_stats (match_id, team, name, rating, goals, assists, tackles_won, tackles_made)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$sql->bind_param("issiiiii", $match_id, $team, $name, $rating, $goals, $assists, $tackles_won, $tackles_made);
$match_id = $_SESSION["match_id"];
$team = mysqli_real_escape_string($link, $_SESSION["team"]);
$name = "test_".$i."_player";
$rating = $_POST["home_player_".$i."_rating_1"];
$goals = $_POST["home_player_".$i."_goals"];
$assists = $_POST["home_player_".$i."_assists"];
$tackles_won = $_POST["home_player_".$i."_tackles_won"];
$tackles_made = $_POST["home_player_".$i."_tackles_made"];
$sql->execute();
$sql->free_result();
}
header("Location: ../match.php?id=".$_SESSION['match_id']);
}
else
{
header("Location: ../match.php?id=".$_SESSION['match_id']);
}
?>
这段代码的作用是通过递增 $i 变量来循环遍历表中的每一行。在每次迭代中,它会将输入框中的数据放入变量中,然后将变量放入准备好的 SQL 语句中。每行的输入框的名称按行号编号,例如:
第 1 行:home_player_1_goals、home_player_1_assists
第 2 行:home_player_2_goals、home_player_2_assists
第 3 行:home_player_3_goals、home_player_3_assists
当我尝试提交此表单时,我收到很多“未识别索引”通知,如下所示:
注意:未定义索引:第 20 行 C:\Apache24\htdocs\script\match_post.php 中的 home_player_1_goals
由于某种原因,它没有找到通过 echo 语句动态插入页面的任何输入框的 POST 变量。查找 $_POST["confirm_result"] 没有问题,因为该输入框一开始就被加载到页面中。
我该如何解决这个问题?它几乎无法识别通过 echo 语句加载的任何输入框。
我还使用 print_r 转储了 $SESSION 和 $POST 变量:
print_r($_SESSION): Array ([email] => my_email@hotmail.com [userid] => 43 [xbox_club] => [ps4_club] => Wrecking Crew [match_id] => 3 [player_count] => 7 [team ] => 家)
print_r($_POST):数组 ([confirm_result] => 确认结果)
它仅显示最初加载到页面上的确认结果按钮。
我还有一个问题:
我将如何根据是否检查来跳过插入行?在每一行中,我都有一个复选框。当此复选框被禁用时,该行输入框将全部被禁用。跳过这一行的最佳方法是什么?目前,无论输入框是否被禁用,我的代码都会遍历每一行并输入数据。
提前致谢!
更新 - 这是单击“发布结果”按钮后生成的单行:
<tr id="player-1">
<td class="match-stats-name"><input type="checkbox" name="home_player_1" value="Syrian2nv" checked="true" class="f-player">Syrian2nv</td>
<td><input name="home_player_1_rating_1" maxlength="1" class="form-input f-stats single"> . <input name="home_player_1_rating_2" maxlength="1" class="form-input f-stats single"></td>
<td><input name="home_player_1_goals" maxlength="1" class="form-input f-stats single"></td>
<td><input name="home_player_1_assists" maxlength="1" class="form-input f-stats single"></td>
<td><input name="home_player_1_tackles_won" maxlength="2" class="form-input f-stats"> / <input name="home_player_1_tackles_made" maxlength="2" class="form-input f-stats"></td>
</tr>