PHP动态列表在下一页中选择值

时间:2013-05-21 04:04:56

标签: php mysql list dynamic insert

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

  

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

     

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

     

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

     

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

     

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

     

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

     

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

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

 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>";
            }

        }

3 个答案:

答案 0 :(得分:1)

您可以设置name属性以选择元素选项。但要将数据发送到脚本,您需要<select>代码的名称,而不是<option>。选项必须仅具有值属性。

答案 1 :(得分:1)

您在name中错误地解释了<select>属性。您必须提供名称属性,如

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

请从<option>中移除名称属性(将其从<option>中移除)并在<select>

中指定

答案 2 :(得分:1)

@NullVoid&amp; @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>";