【发布时间】:2018-06-10 12:02:06
【问题描述】:
我正在尝试将表单中的一些项目插入我的名为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));
}
}
}
这段代码的作用是它基本上获取有关 FIRST SUPPLIER 的所有信息,然后如果用户单击 + 符号,FOR LOOP 将启动并获取所有必需的信息
然后,如果定义的变量不为空,它会在该表中添加一个新的 FIELD。之后,它开始将数组插入到该自定义字段中。
所以它看起来又漂亮又干净,但它会返回很多错误!第一个错误是这样的:
未定义索引:第 11 行的供应商名称_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_sending_
未定义变量:supplier_guarantee_
未定义变量:supplier_price_
未定义变量:supplier_colors_
等
【问题讨论】:
-
您可以使用 $supplier_name_ 。 $p = isset($_POST['supplier_name_'.$p.'']);
-
您在 php 中遇到未定义错误的地方,只需使用 isset()
-
最好使用三元运算符,如果不是未定义则使用其他值跳过
-
@Deep 我试过了,但我仍然得到未定义的变量。另一个更新已提交给该问题。请检查一下。谢谢!
-
这意味着 $p 在循环中没有该迭代的值,即循环没有运行 $p 等于 4 和 5
标签: javascript php sql for-loop mysqli