SQLi插入表中

时间:2016-06-05 07:13:20

标签: php

当我生成下面的代码时,我收到一个错误: 注意:未定义的索引:第47行的C:\ xampp \ htdocs \ cmg-logistics \ make_order.php中的materialcode_1

//insert into database-Line Item Data
$line_item = '';
$more = TRUE;
for ($i=0; $i<5; $i++)
{
      if((isset($_POST['lineitem_'.$i])) && ($_POST['lineitem_'.$i] !="")){
        $line_item = $_POST['lineitem_'.$i];
        $materialcode = $_POST['materialcode_'.$i];
        $query_line_item = "INSERT INTO tbl_order_item (order_number, item, material) values ('$add_nr', '$line_item', 
        '$materialcode')";
        $result_line_item = mysqli_query($con, $query_line_item);
        }else
        {
            $more = FALSE;
        }
}

请帮忙!

1 个答案:

答案 0 :(得分:0)

也许你应该添加一个条件来检查它是否存在:

$_POST['materialcode_'.$i];

例如:

//insert into database-Line Item Data
$line_item = '';
$more = TRUE;
for ($i=0; $i<5; $i++)
{
      if((isset($_POST['lineitem_'.$i])) && ($_POST['lineitem_'.$i] !="") && (isset($_POST['materialcode_'.$i])) && ($_POST['materialcode_'.$i] !="") ){
        $line_item = $_POST['lineitem_'.$i];
        $materialcode = $_POST['materialcode_'.$i];
        $query_line_item = "INSERT INTO tbl_order_item (order_number, item, material) values ('$add_nr', '$line_item', 
        '$materialcode')";
        $result_line_item = mysqli_query($con, $query_line_item);
        }else
        {
            $more = FALSE;
        }
}

错误消息的内容是您获得的数据中不存在$_POST['materialcode_1']