2016-11-08 55 views
0

數我試圖將數據插入表中for循環:插入行和投入使用後

public function insertprolist() 
{  
    for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1) 
    { 
     $data = array(
      'productname' => $this->input->post('proname'+$i), 
      'quantity' => $this->input->post('quantity'+$i), 
      'price' => $this->input->post('price'+$i), 
      'amount' => $this->input->post('amount'+$i) 
     ); 
     return $this->db->insert('purchaseprolist', $data); 
    } 
} 

我得到一個錯誤。

它沒有被插入到數據庫表中。

我已經附加了輸入字段的新行,其名稱proname1quantity1和輸入name屬性會proname2quantity2生成的第二行,並將其繼續下去。

我正在檢索名稱爲numrows的另一個輸入中的行數。

現在我想在模態文件中訪問這些輸入。

+0

請幫我這個:) – Ramya

+2

是什麼問題?錯誤? – Blinkydamo

+0

它沒有被插入數據庫表 – Ramya

回答

2
for ($i = 1; $i < $this->input->post('numrows'); $i++) 
{ 
    $data = array(
       'productname' => $this->input->post('proname' . $i), 
       'quantity' => $this->input->post('quantity' . $i), 
       'price'  => $this->input->post('price' . $i), 
       'amount'  => $this->input->post('amount' . $i) 
       ); 

    $this->db->insert('purchaseprolist', $data); 
} 

這將最有可能的工作但它做的每一行插入,你會得到更好的在看看構建值的數組,並使用insert_batch()做整個事情一次。


更好的選擇:

for ($i = 1; $i < $this->input->post('numrows'); $i++) 
{ 
    $data[] = array(
       'productname' => $this->input->post('proname' . $i), 
       'quantity' => $this->input->post('quantity' . $i), 
       'price'  => $this->input->post('price' . $i), 
       'amount'  => $this->input->post('amount' . $i) 
       ); 
} 
$this->db->insert_batch('purchaseprolist', $data); 

這將執行,而不是循環的行數和執行每行插入一個單一的數據庫插入查詢。相同的數據只會更有效地提交給數據庫。

+0

謝謝你工作:)我從來沒有使用插入批次現在這已經是一個學習:) – Ramya

+1

很高興你的排序。 – Blinkydamo

+0

嗯,那個是完整的例子...... :)我爲你投票。 – devpro

0

也許你應該使用 設置你的"+" = "." PHP會明白這是一個匹配。

for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1) 
{ 
    $data = array(
    'productname' => $this->input->post('proname'.$i), 
    'quantity' => $this->input->post('quantity'.$i), 
    'price' => $this->input->post('price'.$i), 
    'amount' => $this->input->post('amount'.$i) 
); 
    return $this->db->insert('purchaseprolist', $data); 
} 

因爲你的問題不清楚,所以我只能這樣解釋給你。

+0

它不會發生:( – Ramya

+0

雅我的連接錯誤: )謝謝我改變了它,但我也不得不改變我的查詢插入批次:)謝謝你:) – Ramya

-1

在PHP中,您不能使用+號進行連結,你需要使用點這裏(。):

$data = array(
     'productname' => $this->input->post('proname'.$i), 
     'quantity' => $this->input->post('quantity'.$i), 
     'price' => $this->input->post('price'.$i), 
     'amount' => $this->input->post('amount'.$i) 
     ); 

,您使用的回報,這將停止IST後您的功能迭代,你不能在for循環內使用它。

,插入之前,嘗試檢查您的崗位價值你想說什麼,這將幫助你瞭解你有什麼。

print_r($_POST); // before for loop start. 

,而不是多個查詢執行,我建議你使用batch_insert(),這將幫助你:http://www.codeigniter.com/userguide3/database/query_builder.html

+0

謝謝你,我會嘗試檢查:) – Ramya

+0

@Ramya:第三部分對調試非常重要,至少檢查你從你的表格獲得什麼 – devpro

+0

我的連接是錯誤的:)還有當我使用插入批處理它謝謝你:) – Ramya

0

也許問題是您在for循環內試圖return。所以試圖刪除它像下面

public function insertprolist() 
{  
    for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1) 
    { 
     $data = array(
      'productname' => $this->input->post('proname'+$i), 
      'quantity' => $this->input->post('quantity'+$i), 
      'price' => $this->input->post('price'+$i), 
      'amount' => $this->input->post('amount'+$i) 
     ); 
     $this->db->insert('purchaseprolist', $data); 
    } 
} 
+0

錯誤編號:1048 列 '產品名' 不能爲空 INSERT INTO'purchaseprolist'('productname','quantity','price','amount')VALUES(NULL,NULL,NULL,NULL) 文件名:E:/ WAMP/www/CodeIgniter/system/database/DB_driver.php 行數:691 – Ramya

+0

這是我得到的錯誤 – Ramya

+0

然後試試這個''productname'=> $ this-> input-> post('proname'。 $ i),「問題在於你沒有獲得發佈數據 – mapmalith