未定义的索引错误消息php [英] Undefined index error message php

查看:146
本文介绍了未定义的索引错误消息php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从表单中插入一些项目到我的表格中,这个表格叫做 products 。表单看起来像这样:



< img src =https://i.stack.imgur.com/f9e08.pngalt =form>

正如你所看到的,我已经提到了一个添加更多供应商的链接:,它只是向表单添加一个新行。而后面的脚本是这样的:

 < script> 
$(#addbtn)。click(function(){
var next = parseInt($('#counter')。val())+ 1;
$ (+ next +)< th style ='padding-left:10px'>供应商名称(+ next +)<第三方保证第三方保证第三方保证价格第三方保证第三方保证第三方保证价格第三方保证第三方保证第三方保证第三方保证第三方保证第三方保证第三方保证第三方保证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 _ < / input>< / td>< td>< input name ='supplier_price _(+ next +)type ='text'>< / input> ;< / td>< td>< input name ='supplier_colors _(+ next +)'type ='text'>< / input>< / td>< / tr><
$('#counter')。val(next);
});
< / script>

现在我要做的是将用户在表单中输入的所有值数组中的表格。所以为此,我编码:

  //供应商信息
$供应商名称= $ _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);

($ 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); $!
$ b 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_。$ )
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));





这段代码做的是它基本上获得了有关第一供应商的所有信息,然后如果用户点击了 + 标志,FOR LOOP就会启动并获取所有必需的信息



然后,如果定义的变量不是空的,它会在该表中添加一个新的FIELD。之后,它开始将数组插入到该自定义字段。

所以它看起来不错,干净,但它返回了很多错误!第一个错误是:

未定义的索引:第11行的supplier_name_2



第11行去这里:

  $ supplier_name_。 $ p = $ _POST ['supplier_name _'。$ p。''];所有其他错误都是这样的(未定义的索引和未定义变量的每个输入名称,我已经在For循环中指定了。)

所以如果你对此有任何的想法,请让我知道,因为我真的很感激。感谢:)
$ b

更新:



当我把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

现在我得到的错误是:


未定义变量:supplier_name _



未定义变量:supplier_sending _



未定义变量:supplier_guarantee _


$ b 未定义变量:supplier_price _


$ b 未定义变量:supplier_colors _





解决方案

您有 supplier_name _(+ next +)在html中,但是 $ _ POST ['supplier_name _'。$ p。''] 在php中。它不同于()。 var_dump($ _POST);在PHP中,并检查您的HTML输入名称是否与错误生活线上的POST键名相匹配。

更新:



看来你正在使用变量变量。
而不是

  $ supplier_name_。 $ p = isset($ _ POST ['supplier_name _'。$ p。'')); 

try

  $ {'supplier_name_'。 $ p} = isset($ _ POST ['supplier_name _'。$ p。'')); 

  $ {supplier_name_ $ p} = isset($ _ POST ['supplier_name _'。$ p。'']); 

请参阅 http://php.net/manual/en/language.variables.variable.php

适用于supplier_name_和其他适用。


I'm trying to insert some items from a form into my table which is called 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. 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

Then if the defined Variables are not empty, it adds a new FIELD to that table. 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

And the Line 11 goes here:

$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).

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:

    ["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_

Undefined variable: supplier_sending_

Undefined variable: supplier_guarantee_

Undefined variable: supplier_price_

Undefined variable: supplier_colors_

etc

解决方案

You have supplier_name_("+next+") in the html, but $_POST['supplier_name_'.$p.''] in php. It differs the (). var_dump ($_POST); in php and check if your html input names match the POST keynames on the error living lines.

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

Apply to 'supplier_name_' and others if it works.

这篇关于未定义的索引错误消息php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆