English 中文(简体)
Undefined index in PHP
原标题:

I have this error again:

Notice: Undefined index: IDNUMBER in E:wampwwwPHPcreate.php on line 5

Here is my code:

    <?php
        include  E:wampwwwPHPconnection.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:wampwwwPHPmain.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:wampwwwPHPmain.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:wampwwwPHPconnection.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="" />
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.

问题回答

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.

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签