2015-03-03 129 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