gpt4 book ai didi

php - 帖子正在发送空白值

转载 作者:行者123 更新时间:2023-11-30 00:12:03 24 4
gpt4 key购买 nike

我正在尝试将下拉框中的用户输入插入到我的数据库中。我使用此代码作为表单从数据库动态生成选项。它从player1一直持续到player11,但都使用相同的代码,因此出于空间考虑,我将仅包含player1的示例:

<form action="teamCreate.php" onsubmit="return validateForm();" name="teamCreation" method="post">                      
<table>
<tr>
<td>
Player 1:
</td>
<td>
<select id="player1" name="player1">';
$sql = mysql_query("SELECT playerName FROM Player");
while ($row = mysql_fetch_array($sql))
{
echo "<option value=\"player\">" . $row['playerName'] . "</option>";
}
echo '</select>
</td>
</tr>
</table>

这就是我在 teamCreate.php 中使用它所做的事情:

$player=array(); //Creates an array of players, using the values posted from editTeam.php
$player[1]=$_POST['player1'];
$player[2]=$_POST['player2'];
$player[3]=$_POST['player3'];
$player[4]=$_POST['player4'];
$player[5]=$_POST['player5'];
$player[6]=$_POST['player6'];
$player[7]=$_POST['player7'];
$player[8]=$_POST['player8'];
$player[9]=$_POST['player9'];
$player[10]=$_POST['player10'];
$player[11]=$_POST['player11'];

$teamName = $_SESSION['teamName'];
$counter=1;
while ($counter < 12) //Creates a loop that goes through the player array, adding them to the database under the user's team name
{
$currentPlayer = $player['$counter'];
mysql_query("INSERT INTO PlayerListing(teamName, playerName)VALUES('$teamName', '$currentPlayer')");
$counter = $counter + 1;
}

当我查看数据库中的结果时,所有添加的行的 teamName 列均已正确设置,但playerName 列为空。我已经为此困惑了几个小时,但我不明白问题出在哪里。

谢谢,沃尔夫。

最佳答案

看看你的这段代码:

while ($counter < 12) //Creates a loop that goes through the player array, adding them to the database under the user's team name
{
$currentPlayer = $player['$counter'];
mysql_query("INSERT INTO PlayerListing(teamName, playerName)VALUES('$teamName', '$currentPlayer')");
$counter = $counter + 1;
}

当您分配 $currentPlayer 时,您将使用单引号并在其中包含变量名称。看起来您想要就在那里设置 $counter 的值,但是您不能使用单引号来执行此操作,只有双引号才会为您插入变量值。要解决此问题,您必须完全删除引号(这是我建议的)或用双引号将该变量括起来。

while ($counter < 12) //Creates a loop that goes through the player array, adding them to the database under the user's team name
{
$currentPlayer = $player[$counter];
mysql_query("INSERT INTO PlayerListing(teamName, playerName)VALUES('$teamName', '$currentPlayer')");
$counter = $counter + 1;
}

问题是您的 $counter 索引(字面意思是,不是作为变量)不包含任何值,因此没有值被插入到数据库中。

关于php - 帖子正在发送空白值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966661/

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