Undefined index error PHP

前端 未结 9 851
春和景丽
春和景丽 2020-12-01 08:24

I\'m new in PHP and I\'m getting this error:

Notice: Undefined index: productid in /var/www/test/modifyform.php on line 32

Notice: Undef

相关标签:
9条回答
  • 2020-12-01 08:45

    This is happening because your PHP code is getting executed before the form gets posted.

    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

           if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))
            {
                 //process PHP Code
            }
            else
            {
                 //do nothing
             }
    
    0 讨论(0)
  • 2020-12-01 08:46

    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

    0 讨论(0)
  • 2020-12-01 08:48

    There should be the problem, when you generate the <form>. I bet the variables $name, $price are NULL or empty string when you echo them into the value of the <input> field. Empty input fields are not sent by the browser, so $_POST will not have their keys.

    Anyway, you can check that with isset().

    Test variables with the following:

    if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL

    You better set it to NULL, because

    NULL value represents a variable with no value.

    0 讨论(0)
提交回复
热议问题