2

我试图将 php 生成的选项列表中的值插入 mysql 数据库。我在“通知错误”中遇到的错误。错误内容如下:

注意:未定义的索引:第 177 行 C:\xampp\htdocs\project\insert.php 中的 fixture_id

注意:未定义索引:守门员在 C:\xampp\htdocs\project\insert.php 第 178 行

注意:未定义索引:第 179 行 C:\xampp\htdocs\project\insert.php 中的防御者

注意:未定义索引:第 180 行 C:\xampp\htdocs\project\insert.php 中的完整索引

注意:未定义索引:C:\xampp\htdocs\project\insert.php 第 181 行中的 midfielder

注意:未定义的索引:第 182 行 C:\xampp\htdocs\project\insert.php 中的wing

注意:未定义索引:第 183 行 C:\xampp\htdocs\project\insert.php 中的前锋

我的代码如下。我是一名中级开发人员,正在开发团队管理系统。

 elseif(isset($_GET['selection_id'])){ // check if form is submitted
   echo "<table><form name=\"insertSelectionForm\" action=\"". $_SERVER['PHP_SELF']."\" method=\"post\">";
    echo"<tr bgcolor=\"#ccc\"><td colspan=\"2\">Add selection</td></tr>";
    echo"<tr><td>Fixture Id</td><td><select name=\"fixtures[]\">";

    $sql = "SELECT fixture_id FROM `fixtures` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"fixture_id\" value={$rows['fixture_id']} >";
                echo $rows['fixture_id'];
                echo "</option>\n";
            }

    echo "</select>\n";
    echo "</td></tr>";
    echo"<tr><td>Goalkeeper</td><td><select>";
    $sql = "SELECT name FROM `player` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"goalkeeper\" value={$rows['name']}>";
                echo $rows['name'];
                echo "</option>";
            }

    echo "</select>";
    echo "</td></tr>";
    echo"<tr><td>Defender</td><td><select>";

    $sql = "SELECT name FROM `player` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"defender\" value={$rows['name']}>";
                echo $rows['name'];
                echo "</option>";
            }

    echo "</select>";
    echo "</td></tr>";
    echo "<tr><td>Fullback</td><td><select>";

    $sql = "SELECT name FROM `player` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"fullback\" value={$rows['name']}>";
                echo $rows['name'];
                echo "</option>";
            }

    echo "</select>";
    echo "</td></tr>";
    echo"<tr><td>Midfielder</td><td><select>";

    $sql = "SELECT name FROM `player` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"midfielder\" value={$rows['name']}>";
                echo $rows['name'];
                echo "</option>";
            }

    echo "</select>";
    echo "</td></tr>";
    echo"<tr><td>Wing</td><td><select>";

    $sql = "SELECT name FROM `player` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"wing\" value={$rows['name']}>";
                echo $rows['name'];
                echo "</option>";
            }

    echo "</select>";
    echo "</td></tr>";
    echo"<tr><td>Striker</td><td><select>";


    $sql = "SELECT name FROM `player` ";
    $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
                echo "<option name=\"striker\" value={$rows['name']}>";
                echo $rows['name'];
                echo "</option>";
            }

    echo "</select>";
    echo "</td></tr>";
    echo"<tr><td></td><td><input type=\"submit\" value=\"Add Selection\" name=\"selection\"></td></tr>";
    echo "</table></form>";
}


elseif(isset($_POST['selection'])){
            //collect variables posted by form.
            $fixture_id = mysql_real_escape_string($_POST['fixture_id']);
            $goalkeeper = mysql_real_escape_string($_POST['goalkeeper']);
            $defender = mysql_real_escape_string($_POST['defender']);
            $fullback = mysql_real_escape_string($_POST['fullback']);
            $midfielder = mysql_real_escape_string($_POST['midfielder']);
            $wing = mysql_real_escape_string($_POST['wing']);
            $striker = mysql_real_escape_string($_POST['striker']);

            $sql = "SELECT * FROM fixtures WHERE fixture_id = '$fixture_id'";
            $data = mysql_query($sql);
            while($rows = mysql_fetch_array($data)){
            $opponents = $rows['opponents'];    
        }



    //validate form by checking for empty strings that user might have submitted using strlen() php built-in method. If no empty string form processes
         if(strlen($fixture_id)>0 && strlen($goalkeeper)>0 && strlen($defender)>0 && strlen($fullback)>0 && strlen($midfielder)>0 && strlen($wing)>0 && strlen($striker)>0 ){ // if form fields are not empty, insert in database
                $sql = "INSERT into `selections` (fixture_id, opponents, goalkeeper,defender,fullback,midfielder,wing,striker) VALUES('$fixture_id','$opponents', '$goalkeeper','$defender','$fullback','$midfielder','$wing','$striker')";
                $query = mysql_query($sql) or die("Error. ". mysql_error()) ;
                echo "Selection added <br/><br/>";
                echo " <a href=\"team_selections.php\">Go back to team selections page </a>";
            }

        }
4

3 回答 3

1

@NullVoid & @Eugene 确定了最大的问题,但你<form>的也是无效的,因为它不能是<table>

echo "<table><form name=\"insertSelectionForm\" action=\"". $_SERVER['PHP_SELF']."\" method=\"post\">";
      ^^^     ^^^^

<form>标签需要围绕表格 -<form><table>...</table></form>或位于单元格内 -<table><tr><td><form>...</form></td></tr></table>

尝试切换到 -

echo "<form name=\"insertSelectionForm\" action=\"". $_SERVER['PHP_SELF']."\" method=\"post\"><table>";
于 2013-05-21T04:17:18.367 回答
1

您设置name属性以选择元素选项。但是要将数据发送到脚本,您必须为<select>标签设置名称,而不是<option>. 选项必须只有值属性。

于 2013-05-21T04:11:15.423 回答
1

您错误地解释name<select>. 您必须提供名称属性,例如

<select name=\"fixture_id\">而不是在<option name="fixture_id">

<option>所以从(从)中删除名称属性<option>并在<select>

于 2013-05-21T04:11:37.037 回答