简体   繁体   中英

Undefined index error message php

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.

As per your updated question , I believe that loop is ruuning till $p = 3 But you have used static number 5 for condition.

Because value of $p is empty or you can say those does not exists for iteration no. 4 and 5 that is why you are getting issue.

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.

There 5 should be replaced with length variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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