注意:未定义索引,动态网站使用php连接到数据库

注意:未定义索引,动态网站使用php连接到数据库,php,sql,database,Php,Sql,Database,我正在运行一个PHP脚本,不断出现如下错误: 注意:C:\wamp\www\test\updateHW3.php中的未定义索引:ID 116 我使用mysql\u connect()进行学习。这是我的代码: <?php // variables used in script if(isset($_POST[ "submit" ])) { $ID = isset($_POST[ "ID" ]) ? $_POST[ "ID" ] : ""; $fnam

我正在运行一个PHP脚本,不断出现如下错误:

注意:C:\wamp\www\test\updateHW3.php中的未定义索引:ID 116

我使用
mysql\u connect()
进行学习。这是我的代码:

<?php
 // variables used in script
 if(isset($_POST[ "submit" ]))
 {
         $ID = isset($_POST[ "ID" ]) ? $_POST[ "ID" ] : "";
         $fname = isset($_POST[ "fname" ]) ? $_POST[ "fname" ] : "";
         $lname = isset($_POST[ "lname" ]) ? $_POST[ "lname" ] : "";
         $phone = isset($_POST[ "phone" ]) ? $_POST[ "phone" ] : "";
         $birthday = isset($_POST[ "birthday" ]) ? $_POST[ "birthday" ] : "";
         $deathday = isset($_POST[ "deathday" ]) ? $_POST[ "deathday" ] : "";
         $age = isset($_POST[ "age" ]) ? $_POST[ "age" ] : "";
         $gender = isset($_POST[ "gender" ]) ? $_POST[ "gender" ] : "";

          // build INSERT query
           $query = "UPDATE students SET firstname ='&fname',lastname='$lname',telephone='$phone',birthday='$birthday',deathday='$deathday',age='$age',gender='$gender' WHERE S_PK=$ID" ;
        //       echo $query."<br/>";
               // Connect to MySQL
               if ( !( $database = mysql_connect( "localhost", "root", "" ) ) )
                  die( "<p>Could not connect to database</p>" );

               // open MailingList database
               if ( !mysql_select_db( "webdatabase", $database ) )
                  die( "<p>Could not open MailingList database</p>" );

               // execute query in MailingList database
               if ( !( $result = mysql_query( $query, $database ) ) ) 
               {
                  print( "<p>Could not execute query!</p>" );
                  die( mysql_error() );
               } // end if

               mysql_close( $database );
               header("location:readHW3.php");
 }

?>

      <h1>Mailing List Contacts</h1>
      <table>
         <caption>Contacts stored in the database</caption>
         <tr>
            <th>Student_ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>phone</th>
            <th>birthday</th>
            <th>deathday</th>
            <th>age</th>
            <th>gender</th>

         </tr>
         <?php

         print( "<h1>Sample Update Form</h1>
            <p>Please fill in the fields you wish to update and click Register.</p>" );

         print( "<!-- post form data to dynamicForm.php -->
            <form method = 'post' action = ''>
            <h2>User Information</h2>

            <!-- create text boxes for user input -->" );
             // array of name values for the text input fields
         $inputlist = array( "ID" => "Student ID","fname" => "First Name",
            "lname" => "Last Name","phone" => "Phone","birthday" => "Birthday"
            ,"deathday" => "Deathday","age" => "Age");

             // array for gender
         $gender = array( "male",
            "female");

        $ID = isset($_GET[ "ID" ]) ? $_GET[ "ID" ] : "";
        if($ID != "")
        {
            $query = "SELECT * FROM students where S_PK = $ID";

         // Connect to MySQL
         if ( !( $database = mysql_connect( "localhost", "root", "" ) ) )
            die( "<p>Could not connect to database</p></body></html>" );

         // open MailingList database
         if ( !mysql_select_db( "webdatabase", $database ) )
            die( "<p>Could not open MailingList database</p>
               </body></html>" );

         // query MailingList database
         if ( !( $result = mysql_query( $query, $database ) ) ) 
         {
            print( "<p>Could not execute query!</p>" );
            die( mysql_error() . "</body></html>" );
         } // end if

        $row = mysql_fetch_assoc( $result );
        echo "<tr><td>".$ID."</td>";
         foreach ( $inputlist as $inputname => $inputalt)
         {

               echo "<td><input type = 'text' name = '$inputname' value = '" . $row["$inputname"] . "'></td>";



         } // end foreach

        print( "<td><select name = 'gender'>" );          
         foreach ( $gender as $currgender ) 
         {
            print( "<option" .  ($currgender == $row["gender"] ? " selected>" : ">") .  $currgender . "</option>" );
         } // end foreach

         print( "</select></td><td>");



         print( "<td/><tr/></table><!-- create a submit button -->
            <p class = 'head'>
            <input type='hidden' name='ID' value='$ID' />
            <input type = 'submit' name = 'submit'
            value = 'Register'/></p></form></body></html>" );


            mysql_close( $database );
            }
         ?>

您必须更改第116行代码:

<input type='hidden' name='ID' value='$ID' />

使用以下代码行:

<input type='hidden' name='ID' value='".$ID."' />

您必须更改第116行代码:

<input type='hidden' name='ID' value='$ID' />

使用以下代码行:

<input type='hidden' name='ID' value='".$ID."' />

这可能需要更改为以下内容

$row[$inputname]
您可以在其中声明字符串而不是访问变量。看看这是否修复了代码

这可能需要更改为以下内容

$row[$inputname]

您可以在其中声明字符串而不是访问变量。查看这是否修复了代码

第116行有一个错误,因为您似乎只发布了文件的一个片段,所以发布第116行将是有益的:)
$\u GET[“ID”]=$_POST['id']
@MartinHenriksen这是第116行:echo”“;第116行有一个错误,因为您似乎只发布了文件的一个片段,所以发布第116行将是有益的:)
$\u GET[“ID”]=$_POST['id']
@MartinHenriksen这是第116行:echo”“;但我还是有同样的问题。它表示ID、fname、lname、phone的未定义索引。但它与生日、死亡日、性别和年份无关。我不知道为什么?我还是有同样的问题。它表示ID、fname、lname、phone的未定义索引。但它与生日、死亡日、性别和年份无关。我不知道为什么?