PHP 中未定义的索引
我又出现这个错误:
注意:未定义索引:第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
这不适合您的 php 表单处理脚本。试试这个:
基本上,您需要将 name="YXZ" 参数映射到 $_POST['YXZ'] 变量。
This does not fit your php form processing script. Try this:
Basically, you need to map the name="YXZ" params to your $_POST['YXZ'] vars.
看看你的代码。
这条线不应该
是这样的吗
Look at your code.
Shouldn't this line
be something like
既然每个人都指出了这个问题,我只是建议您在这种情况下应该使用
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 ofcreate.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.
当您尝试通过特定数组中不存在的键从数组中获取值时,会发生未定义索引错误。如果你有数组,
并且你尝试这样做
,你将会得到未定义的索引错误。因为
$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
and you try to do
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.