简体   繁体   English

未定义的索引错误信息PHP

[英]Undefined index error message php

I'm trying to insert some items from a form into my table which is called products . 我正在尝试将表格中的某些项目插入称为products表格中。 The form looks like this: 该表格如下所示:

形成

As you can see I have mentioned out a link which is Add more suppliers: + which simply adds a new row to the form. 如您所见,我已经提到了一个链接,该链接是添加更多供应商:+仅向表单添加新行。 And the script behind that is this: 后面的脚本是这样的:

<script>
        $("#addbtn").click(function(){
            var next = parseInt($('#counter').val()) + 1;
            $("#group").append("<table class='table table-hover'><tr><th style='padding-left:10px'>Name of supplier ("+next+")</th><th>Terms of sending</th><th>Guarantee</th><th>Price</th><th>Colors (use , for separation)</th></tr><tr><td style='padding-left:10px'><input name='supplier_name_("+next+")' type='text'></input></td><td><input name='supplier_sending_("+next+")' type='text'></input></td><td><input name='supplier_guarantee_("+next+")' type='text'></input></td><td><input name='supplier_price_("+next+")' type='text'></input></td><td><input name='supplier_colors_("+next+")' type='text'></input></td></tr></table>");
            $('#counter').val(next);
        });
        </script>

Now what I want to do is to insert all the values that user has entered in the form to the table in array . 现在,我要做的是将用户在表单中输入的所有值插入array中的表。 So in order to that, I coded this: 因此,为此,我对此进行了编码:

// supplier info
    $supplier_name_1 = $_POST['supplier_name_1'];
    $supplier_sending_1 = $_POST['supplier_sending_1'];
    $supplier_guarantee_1 = $_POST['supplier_guarantee_1'];
    $supplier_price_1 = $_POST['supplier_price_1'];
    $supplier_colors_1 = $_POST['supplier_colors_1'];
    $supplier_info_1 = array($supplier_name_1, $supplier_sending_1, $supplier_guarantee_1, $supplier_price_1, $supplier_colors_1);
    $proSupply_1 = json_encode($supplier_info_1);

    for($p=2;$p<=5;$p++){
        $supplier_name_ . $p = isset($_POST['supplier_name_'.$p.'']); 


$supplier_sending_ . $p = isset($_POST['supplier_sending_'.$p.'']);
            $supplier_guarantee_ . $p = isset($_POST['supplier_guarantee_'.$p.'']);
            $supplier_price_ . $p = isset($_POST['supplier_price_'.$p.'']);
            $supplier_colors_ . $p = isset($_POST['supplier_colors_'.$p.'']);
            $supplier_info_ . $p = array($supplier_name_ . $p, $supplier_sending_ . $p, $supplier_guarantee_ . $p, $supplier_price_ . $p, $supplier_colors_ . $p);
            $proSupply_ . $p = json_encode($supplier_info_ . $p);
            $supplier_info_ . $p = array($supplier_name_ . $p, $supplier_sending_ . $p, $supplier_guarantee_ . $p, $supplier_price_ . $p, $supplier_colors_ . $p);
            $proSupply_ . $p = json_encode($supplier_info_ . $p);

            if(!empty($supplier_name_ . $p)&&(!empty($supplier_sending_ . $p))&&(!empty($supplier_guarantee_ . $p))&&(!empty($supplier_price_ . $p))&&(!empty($supplier_colors_ . $p))){
                if($p == 2){
                    $sql = "ALTER TABLE products ADD '.$supplier_name_ . $p.' varchar(500) AFTER supplier_info_1";
                }else if($p > 2){
                    $i = $p-1;
                    $sql = "ALTER TABLE products ADD '.$supplier_name_ . $p.' varchar(500) AFTER supplier_info_".$i;
                }
                if ($con->query($sql) === TRUE){
                    $insert_suppliers = "
                    INSERT INTO `products` (`supplier_info_" . $p . "`) 
                    VALUES ('$proSupply_" . $p . "')
                    ";
                    $run_suppliers = mysqli_query($con,$insert_suppliers);
                    if(!$run_product){
                        error_reporting(E_ALL);
                        die(mysqli_error($con));
                    }
                }else{
                    error_reporting(E_ALL);
                    die(mysqli_error($con));
                }
            }
        }

What this code does is that it basically gets all the information about the FIRST SUPPLIER and then if the user has clicked the + sign, a FOR LOOP starts and gets all the required information 该代码的作用是,它基本上获取了有关第一个供应商的所有信息,然后,如果用户单击了+号,则FOR LOOP将启动并获取所有必需的信息。

Then if the defined Variables are not empty, it adds a new FIELD to that table. 然后,如果定义的变量不为空,则向该表添加新的FIELD。 After that, it starts inserting the array to that custom field. 之后,它将开始将数组插入该自定义字段。

So it looks nice and clean but it returns lots of errors! 因此,它看起来很干净,但会返回很多错误! The 1st error is this: 第一个错误是这样的:

Undefined index: supplier_name_2 on line 11 未定义的索引:第11行的Supplier_name_2

And the Line 11 goes here: 11号线在这里:

$supplier_name_ . $p = $_POST['supplier_name_'.$p.''];

All the other errors are like this one (Undefined Index & Undefined Variable of every input names that I have specified in the For Loop). 所有其他错误都与此类似(我在For循环中指定的每个输入名称的未定义索引和未定义变量)。

So if you have any idea about this, please let me know, cause I really appreciate that. 因此,如果您对此有任何想法,请告诉我,因为我真的很感激。 Thanks :) 谢谢 :)

UPDATE: 更新:

When I put var_dump($_POST), I get this: 当我放var_dump($ _ POST)时,我得到了:

    ["supplier_name_1"]=>
  string(10) "supplier 1"
  ["supplier_sending_1"]=>
  string(7) "terms 1"
  ["supplier_guarantee_1"]=>
  string(11) "guarantee 1"
  ["supplier_price_1"]=>
  string(7) "price 1"
  ["supplier_colors_1"]=>
  string(8) "colors 1"
  ["supplier_name_2"]=>
  string(10) "supplier 2"
  ["supplier_sending_2"]=>
  string(7) "terms 2"
  ["supplier_guarantee_2"]=>
  string(11) "guarantee 2"
  ["supplier_price_2"]=>
  string(7) "price 2"
  ["supplier_colors_2"]=>
  string(8) "colors 2"
  ["supplier_name_3"]=>
  string(10) "supplier 3"
  ["supplier_sending_3"]=>
  string(7) "terms 3"
  ["supplier_guarantee_3"]=>
  string(11) "guarantee 3"
  ["supplier_price_3"]=>
  string(7) "price 3"
  ["supplier_colors_3"]=>
  string(8) "colors 3"
  ["counter"]=>
  string(1) "3"

And the error that I get now are these: 我现在得到的错误是:

Undefined variable: supplier_name_ 未定义的变量:Supplier_name_

Undefined variable: supplier_sending_ 未定义的变量:vendor_sending_

Undefined variable: supplier_guarantee_ 未定义的变量:vendor_guarantee_

Undefined variable: supplier_price_ 未定义的变量:vendor_price_

Undefined variable: supplier_colors_ 未定义的变量:vendor_colors_

etc 等等

You have supplier_name_("+next+") in the html, but $_POST['supplier_name_'.$p.''] in php. 您在html中有supplier_name_("+next+") ,但在php中有$_POST['supplier_name_'.$p.''] It differs the (). 它与()不同。 var_dump ($_POST) ; var_dump ($_POST) ; in php and check if your html input names match the POST keynames on the error living lines. 在php中,检查您的html输入名称是否与错误提示行上的POST关键字名称匹配。

Update: 更新:

It seems you're using variable variables. 看来您正在使用变量变量。 Instead of 代替

$supplier_name_ . $p = isset($_POST['supplier_name_'.$p.'']);

try 尝试

${'supplier_name_' . $p} = isset($_POST['supplier_name_'.$p.'']);

or 要么

${"supplier_name_$p"} = isset($_POST['supplier_name_'.$p.'']);

See http://php.net/manual/en/language.variables.variable.php 参见http://php.net/manual/en/language.variables.variable.php

Apply to 'supplier_name_' and others if it works. 如果适用,请套用至'supplier_name_'及其他。

As per your updated question , I believe that loop is ruuning till $p = 3 But you have used static number 5 for condition. 根据您更新的问题,我相信循环将持续到$ p = 3,但您已使用静态数字5作为条件。

Because value of $p is empty or you can say those does not exists for iteration no. 因为$ p的值为空,或者您可以说在迭代号no中不存在这些值。 4 and 5 that is why you are getting issue. 4和5,这就是为什么您遇到问题。

you should get the exact length and use that rather than use $p<=5 directly, just modify this condition in loop , replace 5 with dynamic number. 您应该获取确切的长度并使用该长度,而不是直接使用$ p <= 5,只需在循环中修改此条件,然后将5替换为动态数字即可。

There 5 should be replaced with length variable. 应该用长度变量替换5。

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

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