我试图将 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>";
}
}