简体   繁体   English

未定义的索引错误PHP

[英]Undefined index error PHP

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 注意:未定义的索引:第32行的/var/www/test/modifyform.php中的productid

Notice: Undefined index: name in /var/www/test/modifyform.php on line 33 注意:未定义的索引:第33行的/var/www/test/modifyform.php中的名称

Notice: Undefined index: price in /var/www/test/modifyform.php on line 34 注意:未定义的索引:第34行/var/www/test/modifyform.php中的价格

Notice: Undefined index: description in /var/www/test/modifyform.php on line 35 注意:未定义的索引:第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. 之后我(或者至少我正在尝试)做的是更新MySQL中的表。 I really can't understand why $rowID is defined while the other variables aren't. 我真的不明白为什么定义$rowID而其他变量不定义。

Thank you for taking your time to answer me. 感谢您抽出宝贵时间回答我。 Cheers! 干杯!

Try: 尝试:

<?php

if (isset($_POST['name'])) {
    $name = $_POST['name'];
}

if (isset($_POST['price'])) {
    $price = $_POST['price'];
}

if (isset($_POST['description'])) {
    $description = $_POST['description'];
}

?>

Apparently the index 'productid' is missing from your html form. 显然你的html表单中缺少索引'productid'。 Inspect your html inputs first. 首先检查你的html输入。 eg <input type="text" name="productid" value=""> But this will handle the current error PHP is raising. 例如<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'] : '';

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'];

}

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
         }

There should be the problem, when you generate the <form> . 生成<form>时应该存在问题。 I bet the variables $name , $price are NULL or empty string when you echo them into the value of the <input> field. 我打赌变量$name$priceNULL或空字符串,当你将它们echo<input>字段的value时。 Empty input fields are not sent by the browser, so $_POST will not have their keys. 浏览器不会发送空输入字段,因此$_POST将不会有其密钥。

Anyway, you can check that with isset() . 无论如何,您可以使用isset()检查。

Test variables with the following: 测试变量如下:

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值表示没有值的变量。

嘿这发生了,因为你试图在赋值之前显示价值你只需填写值并提交表格就会显示正确的输出或者你可以写下你的PHP代码下面的表格标签它会运行没有任何错误

If you are using wamp server , then i recommend you to use xampp server . 如果您使用的是wamp服务器,那么我建议您使用xampp服务器。 you . i get this error in less than i minute but i resolved this by using (isset) function . 我在不到一分钟内得到这个错误,但我通过使用(isset)函数解决了这个问题。 and i get no error . 我没有得到任何错误。 and after that i remove (isset) function and i don,t see any error. 然后我删除(isset)功能,我不知道,看到任何错误。

by the way i am using xampp server 顺便说一句,我正在使用xampp服务器

this error occurred sometime method attribute ( valid passing method ) Error option : method="get" but called by $Fname = $_POST["name"]; 某些时候发生此错误的方法属性(有效传递方法)错误选项:method =“get”但由$ Fname = $ _POST [“name”]调用; or 要么

       method="post" but  called by  $Fname = $_GET["name"];

More info visit http://www.doordie.co.in/index.php 更多信息,请访问http://www.doordie.co.in/index.php

To remove this error, in your html form you should do the following in enctype : 要删除这个错误,在你的HTML表单,你应该做好以下enctype

<form  enctype="multipart/form-data">

The following down is the cause of that error ie if you start with form-data in enctype, so you should start with multipart: 以下是导致该错误的原因,即如果您从enctype中的form-data开始,那么您应该从multipart开始:

<form enctype="form-data/multipart">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM