开发者

Undefined index in PHP

I have this error again:

Notice: Undefined index: IDNUMBER in E:\wamp\www\PHP\create.php on line 5

Here is my code:

    <?php
        include 'E:\wamp\www\PHP\connection.php';

        $IDNUMBER = $_POST['IDNUMBER'];
        $LNAME    = $_POST['LNAME'];
        $FNAME    = $_POST['FNAME'];
        $MNAME    = $_POST['MNAME'];
        $GRADEYR  = $_POST['GRADEYR'];
        $ADDRESS  = $_POST['ADDRESS'];
        if(!$_POST['submit']) {
            echo "please fill out the form";
            header('E:\wamp\www\PHP\main.php');
         } else {
             mysql_query("INSERT INTO students(`IDNUMBER`,`LNAME`,`FNAME`,`MNAME`,`GRADEYR`,`ADDRESS`)
                          VALUES (NULL, '$IDNUMBER', '$LNAME', '$FNAME', '$MNAME', '$GRADEYR', '$ADDRESS')") or die(mysql_error());

             echo "User has been added!";
             header('E:\wamp\www\PHP\main.php');

         }   ?>

Here is my main.php:

    <html xmlns="http://www.w3.org/1999/xhtml">

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Untitled Document</title>
        </head>

            <?php
                include 'E:\wamp\www\PHP\connection.php';
                $query = "SELECT * FROM students";
                $result = mysql_query($query);
                while($students = mysql_fetch_array($result)) {
                    echo "<h3>".$students['LNAME'] ."</H3>";
                }
            ?>
            <h1>Create A user</h1>
                <form action="create.php" method="post">
                    Idnumber:<input    type="text"   name="LName"       value="" />
                    LastName:<input    type="text"   name="LName"       value="" />
                    Firstname:<input   type="text"   name="FName"       value="" />
                    Middlename:<input  type="text"   name="MName"       value="" />
                    GradeOrYear:<input type="text"   name="GradeOrYear" value="" />开发者_如何转开发;
                    Address:<input     type="text"   name="Address"     value="" />
                    <br/>
                    <input             type="submit" name="Submit"      value="" />
                </form>
            <body>
            </body>
        </html>

And my connection.php:

    <?php
        $dbhost = 'localhost';
        $dbuser= 'root';
        $dbpass= '';
        $dbname = 'koro';

        $conn =mysql_connect($dbhost, $dbuser, $dbpass);
        mysql_select_db($dbname);
    ?>

*Where am I supposed to define the undefined in here?


Idnumber:<input type="text" name="LName" value="" />
Isn't this supposed to be named IDNUMBER?

The "Undefined index" warning just tells you that you are assuming an index in an array exists, when in fact it does not.

The first thing you should do in these cases is to find out where said array index is created (the form, in this case) and make sure everything is in order. These errors are usually just typos.


Idnumber:<input type="text" name="LName" value="" />
LastName:<input type="text" name="LName" value="" />
Firstname:<input type="text" name="FName" value="" />
Middlename:<input type="text" name="MName" value="" />
GradeOrYear:<input type="text" name="GradeOrYear" value="" />
Address:<input type="text" name="Address" value="" />
<br/>
   <input type="submit" name="Submit" value="" />

This does not fit your php form processing script. Try this:

Idnumber:<input type="text" name="IDNUMBER" value="" />
LastName:<input type="text" name="LNAME" value="" />
Firstname:<input type="text" name="FNAME" value="" />
Middlename:<input type="text" name="MNAME" value="" />
GradeOrYear:<input type="text" name="GRADEYR" value="" />
Address:<input type="text" name="ADDRESS" value="" />
<br/>
   <input type="submit" name="submit" value="submit" />

Basically, you need to map the name="YXZ" params to your $_POST['YXZ'] vars.


Look at your code. Shouldn't this line

Idnumber:<input type="text" name="LName" value="" />

be something like

Idnumber:<input type="text" name="IDNUMBER" value="" />


Since everyone pointed out the problem I'll just suggest that you should dump out the array in cases like this using var_dump($_POST) at the top of create.php to see what's actually in it. If you do that you'll see that there is no IDNUMBER.

Also, I might just be too picky but you uppercased all the indexes like LNAME, FNAME, etc. when they aren't defined as such in the form's HTML as such. It's best to be consistent regardless if the language lets you get away without it.


Undefined index error occurs when you try to get value from the array by the key which does not exist in that particular array. If you have array, say

$array = array('key' => 'value', 'another_key' => 'another_value');

and you try to do

print $array['yet_another_key'];

you are going to get undefined index error. Because 'yet_another_key' key does not exist in the $array array.

So now you should understand what exactly this error means, and also you have an error line number so it's not going to be hard to figure out which array is using a non-existing key. And fix it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜