[英]Undefined index error message php
我正在嘗試將表格中的某些項目插入稱為products
表格中。 該表格如下所示:
如您所見,我已經提到了一個鏈接,該鏈接是添加更多供應商:+僅向表單添加新行。 后面的腳本是這樣的:
<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>
現在,我要做的是將用戶在表單中輸入的所有值插入array中的表。 因此,為此,我對此進行了編碼:
// 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));
}
}
}
該代碼的作用是,它基本上獲取了有關第一個供應商的所有信息,然后,如果用戶單擊了+號,則FOR LOOP將啟動並獲取所有必需的信息。
然后,如果定義的變量不為空,則向該表添加新的FIELD。 之后,它將開始將數組插入該自定義字段。
因此,它看起來很干凈,但會返回很多錯誤! 第一個錯誤是這樣的:
未定義的索引:第11行的Supplier_name_2
11號線在這里:
$supplier_name_ . $p = $_POST['supplier_name_'.$p.''];
所有其他錯誤都與此類似(我在For循環中指定的每個輸入名稱的未定義索引和未定義變量)。
因此,如果您對此有任何想法,請告訴我,因為我真的很感激。 謝謝 :)
更新:
當我放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_
未定義的變量:vendor_sending_
未定義的變量:vendor_guarantee_
未定義的變量:vendor_price_
未定義的變量:vendor_colors_
等等
您在html中有supplier_name_("+next+")
,但在php中有$_POST['supplier_name_'.$p.'']
。 它與()不同。 var_dump ($_POST)
; 在php中,檢查您的html輸入名稱是否與錯誤提示行上的POST關鍵字名稱匹配。
更新:
看來您正在使用變量變量。 代替
$supplier_name_ . $p = isset($_POST['supplier_name_'.$p.'']);
嘗試
${'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_'及其他。
根據您更新的問題,我相信循環將持續到$ p = 3,但您已使用靜態數字5作為條件。
因為$ p的值為空,或者您可以說在迭代號no中不存在這些值。 4和5,這就是為什么您遇到問題。
您應該獲取確切的長度並使用該長度,而不是直接使用$ p <= 5,只需在循環中修改此條件,然后將5替換為動態數字即可。
應該用長度變量替換5。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.