帖子发送空白值

时间:2014-05-31 05:19:14

标签: php mysql database

我正在尝试将用户输入从下拉框插入到我的数据库中。我使用此代码表单从数据库动态生成选项。并且它继续播放器1到播放器11,但是所有都使用相同的代码,因此我将仅为了空间的利益包含一个播放器1的示例:

<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列为空。几个小时我一直在困惑,我不明白问题是什么。

谢谢,沃尔夫。

1 个答案:

答案 0 :(得分:0)

看一下你的代码块:

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索引(字面意思,不作为变量)不包含任何值,因此没有值插入到数据库中。