PHP 中未定义的索引

发布于 2024-08-18 07:27:12 字数 2748 浏览 4 评论 0原文

我又出现这个错误:

注意:未定义索引:第 5 行 E:\wamp\www\PHP\create.php 中的 IDNUMBER

这是我的代码:

    <?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');

         }   ?>

这是我的 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>

和我的 connection.php:

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

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

*我应该在哪里定义未定义的这里?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

节枝 2024-08-25 07:27:12

Idnumber:
这不是应该命名为IDNUMBER吗?

“未定义索引”警告只是告诉您,您假设数组中存在索引,但实际上并不存在。

在这些情况下,您应该做的第一件事是找出创建所述数组索引的位置(在本例中为表单)并确保一切都按顺序进行。这些错误通常只是拼写错误。

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.

束缚m 2024-08-25 07:27:12
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="" />

这不适合您的 php 表单处理脚本。试试这个:

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" />

基本上,您需要将 name="YXZ" 参数映射到 $_POST['YXZ'] 变量。

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.

恏ㄋ傷疤忘ㄋ疼 2024-08-25 07:27:12

看看你的代码。
这条线不应该

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

是这样的吗

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

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="" />
坦然微笑 2024-08-25 07:27:12

既然每个人都指出了这个问题,我只是建议您在这种情况下应该使用 create.php 顶部的 var_dump($_POST) 转储数组看看里面到底有什么。如果这样做,您将看到没有 IDNUMBER。

另外,我可能太挑剔了,但是当 LNAME、FNAME 等索引在表单的 HTML 中没有这样定义时,您将所有索引都大写了。最好保持一致,无论该语言是否允许您在没有它的情况下逃脱。

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.

ペ泪落弦音 2024-08-25 07:27:12

当您尝试通过特定数组中不存在的键从数组中获取值时,会发生未定义索引错误。如果你有数组,

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

并且你尝试这样做

print $array['yet_another_key'];

,你将会得到未定义的索引错误。因为 $array 数组中不存在 'yet_another_key' 键。

所以现在您应该了解这个错误到底意味着什么,并且您还有一个错误行号,因此不难找出哪个数组正在使用不存在的键。并修复它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文