2015-03-03 127 views
0

我是新来的PHP我试图在数据库中插入多个值。我写了我的函数,但它抛出一个错误。我附上了我的HTML表格下面。我该如何解决这个问题...在数据库中插入多个值

错误显示:

A PHP Error was encountered 
Severity: Notice 
Message: Undefined index: itemNo 
Filename: controllers/main.php 
Line Number: 2406 

我的代码:

public function item_save(){ 
    $con = mysql_connect("localhost","root",""); 
    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("inventory", $con); 
    //$id_number = $_GET['id_user']; 
    $val1=$_GET['itemNo']; 
    $val2=$_GET['itemName']; 
    $val3=$_GET['quantity']; 
    $val4=$_GET['price']; 
    $val5=$_GET['total']; 
    $val6=$_GET['subTotal']; 
    $val7=$_GET['tax']; 
    $val8=$_GET['taxAmount']; 
    $val9=$_GET['totalAftertax']; 
    $val10=$_GET['amountPaid']; 
    $val1=$_GET['amountDue']; 
    $N = count($val1); 
    for($i=0; $i < $N; $i++) 
    { 
    mysql_query("INSERT INTO item_save(item_no,item_name,qty,price,total,subtotal,tax,tax_amount,total_final,amount_paid,amount_due) VALUES ('$val1[$i]','$val2[$i]','$val3[$i]','$val4[$i]','$val5[$i]','$val6','$val7','$val8','$val9','$val10','$val11')"); 
    } 
    redirect("main/multiple"); 

     } 

form.html

<div class="container content"> 
    <?php 

echo form_open_multipart('main/item_save'); 


?> 
     <?php 


$submit=array(

    'name'=>'submit', 
    'type'=>'submit', 
    'class'=>'btn btn-primary', 
    'value'=>'Submit', 

); 
?> 
     <div class='row'> 
      <div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'> 
       <table class="table table-bordered table-hover"> 
        <thead> 
         <tr> 
          <th width="2%"><input id="check_all" class="formcontrol" type="checkbox"/></th> 
          <th width="15%">Item No</th> 
          <th width="38%">Item Name</th> 
          <th width="15%">Price</th> 
          <th width="15%">Quantity</th> 
          <th width="15%">Total</th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr> 
          <td><input class="case" type="checkbox"/></td> 
          <td><input type="text" data-type="productCode" name="itemNo[]" id="itemNo_1" class="form-control autocomplete_txt" autocomplete="off"></td> 
          <td><input type="text" data-type="productName" name="itemName[]" id="itemName_1" class="form-control autocomplete_txt" autocomplete="off"></td> 
          <td><input type="number" name="price[]" id="price_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td> 
          <td><input type="number" name="quantity[]" id="quantity_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td> 
          <td><input type="number" name="total[]" id="total_1" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
     </div> 
     <div class='row'> 
      <div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'> 
       <button class="btn btn-danger delete" type="button">- Delete</button> 
       <button class="btn btn-success addmore" type="button">+ Add More</button> 
      </div> 
      <div class='col-xs-12 col-sm-offset-4 col-md-offset-4 col-lg-offset-4 col-sm-5 col-md-5 col-lg-5'> 
       <form class="form-inline"> 
        <div class="form-group"> 
         <label>Subtotal: &nbsp;</label> 
         <div class="input-group"> 
          <div class="input-group-addon">$</div> 
                 <input type="number" class="form-control" id="subTotal" name="subTotal" placeholder="Subtotal" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label>Tax: &nbsp;</label> 
         <div class="input-group"> 
          <div class="input-group-addon">$</div> 
                 <input type="number" class="form-control" id="tax" name="tax" placeholder="Tax" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label>Tax Amount: &nbsp;</label> 
         <div class="input-group"> 
          <input type="number" class="form-control" id="taxAmount" name="taxAmount" placeholder="Tax" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"> 
          <div class="input-group-addon">%</div> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label>Total: &nbsp;</label> 
         <div class="input-group"> 
          <div class="input-group-addon">$</div> 
                 <input type="number" class="form-control" id="totalAftertax" name="totalAftertax" placeholder="Total" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label>Amount Paid: &nbsp;</label> 
         <div class="input-group"> 
          <div class="input-group-addon">$</div> 
                 <input type="number" class="form-control" id="amountPaid" name="amountPaid" placeholder="Amount Paid" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label>Amount Due: &nbsp;</label> 
         <div class="input-group"> 
          <div class="input-group-addon">$</div> 
                 <input type="number" class="form-control amountDue" id="amountDue" name="amountDue" placeholder="Amount Due" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"> 
         </div> 
        </div> 
       </form> 

      </div> 
      <?php echo form_submit($submit);?> 
     </div> 

    </div> 
+0

是否item_no是您的主要ID – 2015-03-03 07:59:27

+0

请勿使用已弃用的'mysqli_ *'API。使用'mysqli_''或pdo与准备好的语句。 – Jens 2015-03-03 08:00:34

+0

错误信息很清楚,没有'$ _GET ['itemNo']'。你是用'method = GET'还是'method = POST'发布了你的表单?如果它是'POST',那么你需要使用'$ _POST'而不是'$ _GET'。 – Barmar 2015-03-03 08:00:35

回答

1

Notice: Undefined index: itemNo表示您的$_GET['itemNo'];未设置。

您可能忘了在您的网址中添加&itemNo=xxx,但是查看您缺少的其他代码itemNo[]从窗体获取参数(我猜是复选框)。

编辑:
现在,您发布HTML后,我仍然没有看到<form>标签,让我问你。你确定你正在使用GET方法吗?因为多部分表格通常使用POST方法。

检查您的HTML输出(作为浏览器中的源代码)或某些网络调试,并检查它是POST还是GET。我敢打赌,这是POST,但你尝试检索GET。

此外,您可以尝试更改$_GET$_REQUEST(同时匹配GET和POST),但这不是最好的主意。

+0

它应该是'&itemNo [] = xxx',因为他以数组的形式访问它。 – Barmar 2015-03-03 08:05:03

+0

如果他提交表单,浏览器会自动执行此操作。 – Barmar 2015-03-03 08:05:44

+0

是的,忘记了。除非我们看到HTML,否则很难说发生了什么。无论如何,通知来自'$ _GET ['itemNo']'所以它可能完全缺失。 – AkeLio 2015-03-03 08:07:06