未定义索引错误 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10613570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined index error PHP
提问by LPoblet
I'm new in PHP and I'm getting this error:
我是 PHP 新手,出现此错误:
Notice: Undefined index: productid in /var/www/test/modifyform.php on line 32
Notice: Undefined index: name in /var/www/test/modifyform.php on line 33
Notice: Undefined index: price in /var/www/test/modifyform.php on line 34
Notice: Undefined index: description in /var/www/test/modifyform.php on line 35
注意:未定义索引:32 行 /var/www/test/modifyform.php 中的 productid
注意:未定义索引:第 33 行 /var/www/test/modifyform.php 中的名称
注意:未定义索引:34 行 /var/www/test/modifyform.php 中的价格
注意:未定义索引:第 35 行 /var/www/test/modifyform.php 中的描述
I couldn't find any solution online, so maybe someone can help me.
我在网上找不到任何解决方案,所以也许有人可以帮助我。
Here is the code:
这是代码:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="rowID" value="<?php echo $rowID;?>">
<p>
Product ID:<br />
<input type="text" name="productid" size="8" maxlength="8" value="<?php echo $productid;?>" />
</p>
<p>
Name:<br />
<input type="text" name="name" size="25" maxlength="25" value="<?php echo $name;?>" />
</p>
<p>
Price:<br />
<input type="text" name="price" size="6" maxlength="6" value="<?php echo $price;?>" />
</p>
<p>
Description:<br />
<textarea name="description" rows="5" cols="30">
<?php echo $description;?></textarea>
</p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</form>
<?php
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
What I do after that (or at least I'm trying) is to update a table in MySQL.
I really can't understand why $rowIDis defined while the other variables aren't.
之后我所做的(或者至少我正在尝试)是更新 MySQL 中的表。我真的不明白为什么$rowID定义了而其他变量没有。
Thank you for taking your time to answer me. Cheers!
感谢您抽出宝贵时间回答我。干杯!
回答by Adam
Try:
尝试:
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['price'])) {
$price = $_POST['price'];
}
if (isset($_POST['description'])) {
$description = $_POST['description'];
}
?>
回答by Robert Wilson
Apparently the index 'productid' is missing from your html form.
Inspect your html inputs first. eg <input type="text" name="productid" value="">But this will handle the current error PHP is raising.
显然,您的 html 表单中缺少索引“productid”。首先检查您的 html 输入。例如,<input type="text" name="productid" value="">但这将处理 PHP 引发的当前错误。
$rowID = isset($_POST['rowID']) ? $_POST['rowID'] : '';
$productid = isset($_POST['productid']) ? $_POST['productid'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$price = isset($_POST['price']) ? $_POST['price'] : '';
$description = isset($_POST['description']) ? $_POST['description'] : '';
回答by Akshat Maltare
This is happening because your PHP code is getting executed before the form gets posted.
发生这种情况是因为您的 PHP 代码在表单发布之前就被执行了。
To avoid this wrap your PHP code in following if statement and it will handle the rest no need to set if statements for each variables
为避免这种情况,将您的 PHP 代码包装在以下 if 语句中,它将处理其余部分,无需为每个变量设置 if 语句
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))
{
//process PHP Code
}
else
{
//do nothing
}
回答by sumish1985
TRY
尝试
<?php
$rowID=$productid=$name=$price=$description="";
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
回答by Geroge
If you are using wamp server , then i recommend you to use xampp server . you . i get this error in less than i minute but i resolved this by using (isset) function . and i get no error . and after that i remove (isset) function and i don,t see any error.
如果您使用的是 wamp 服务器,那么我建议您使用 xampp 服务器。你 。我在不到一分钟的时间内收到此错误,但我使用 (isset) 函数解决了此问题。我没有错误。之后我删除了 (isset) 函数,我没有看到任何错误。
by the way i am using xampp server
顺便说一下,我正在使用 xampp 服务器
回答by Dyin
There should be the problem, when you generate the <form>. I bet the variables $name, $priceare NULLor empty string when you echothem into the valueof the <input>field. Empty input fields are not sent by the browser, so $_POSTwill not have their keys.
应该有问题,当您生成<form>. 我敢打赌$name,当您进入该领域时,变量$price是NULL或空字符串。浏览器不会发送空输入字段,因此不会有它们的键。echovalue<input>$_POST
Anyway, you can check that with isset().
无论如何,您可以使用isset().
Test variables with the following:
使用以下测试变量:
if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL
if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL
You better set it to NULL, because
你最好把它设置为NULL,因为
NULL value represents a variable with no value.
NULL 值表示没有值的变量。
回答by rohit sonawane
Hey this is happening because u r trying to display value before assignnig it U just fill in the values and submit form it will display correct output Or u can write ur php code below form tags It ll run without any errors
嘿,这是因为你试图在分配之前显示值你只需填写值并提交表单它会显示正确的输出或者你可以在表单标签下编写你的php代码它会运行没有任何错误
回答by OpenWebWar
this error occurred sometime method attribute ( valid passing method ) Error option : method="get" but called by $Fname = $_POST["name"]; or
this error occurred sometime method attribute ( valid passing method ) Error option : method="get" but called by $Fname = $_POST["name"]; or
method="post" but called by $Fname = $_GET["name"];
More info visit http://www.doordie.co.in/index.php
More info visit http://www.doordie.co.in/index.php
回答by Omary
To remove this error, in your html form you should do the following in enctype:
To remove this error, in your html form you should do the following in enctype:
<form enctype="multipart/form-data">
The following down is the cause of that error i.e if you start with form-data in enctype, so you should start with multipart:
The following down is the cause of that error i.e if you start with form-data in enctype, so you should start with multipart:
<form enctype="form-data/multipart">

