2016-11-17 53 views
1
public function insertpricinglist() 
    { 
    $wholesale = "wholesale"; 
    $dealer = "dealer"; 
    $customer = "customer"; 
$query = $this->db->get_where('products', array('productname' => $this->input->post('productname'))); 
    $row = $query->result(); 
    $temp = $row->id; 

for ($i = 1; $i < $this->input->post('numrows'); $i++) 
{ 

$data[] = array(
      'product_id' => $temp, 
      'range'  => $this->input->post('range' . $i), 
      '[email protected]'  => $this->input->post('vat' . $i), 
      'price'  => $this->input->post('amount' . $i), 
      'uom'  => $this->input->post('uom' . $i), 
      'usertype' => $wholesale 
      ); 
    } 
$this->db->insert_batch('product_pricing', $data); 
for ($i = 1; $i < $this->input->post('dealer_numrows'); $i++) 
{ 

    $data[] = array(
      'product_id' => $temp, 
      'range'  => $this->input->post('dealerrange' . $i), 
      '[email protected]'  => $this->input->post('dealervat' . $i), 
      'price'  => $this->input->post('dealeramount' . $i), 
      'uom'  => $this->input->post('dealeruom' . $i), 
      'usertype' => $dealer 
      ); 
} 
$this->db->insert_batch('product_pricing', $data); 
for ($i = 1; $i < $this->input->post('customer_numrows'); $i++) 
{ 

$data[] = array(
      'product_id' => $temp, 
      'price'  => $this->input->post('customer_amount' . $i), 
      'uom'  => $this->input->post('customer_uom' . $i), 
      'usertype' => $customer 
      ); 
} 
$this->db->insert_batch('product_pricing', $data); 
} 

我在嘗試插入我的一批數據時出現錯誤。我試圖插入產品定價表中批量批次和批發商批次和客戶批次項目插入相同的產品ID。Codeigniter - 插入批處理 - 我的sql

Error Number: 1064 

You have an error in your SQL syntax; check the manual that corresponds to  your MySQL server version for the right syntax to use near 'Array' at line 1 

// 嚴重性:注意

Message: Trying to get property of non-object 

Filename: models/productmodel.php 

Line Number: 24 

    Backtrace: 

File: E:\wamp\www\CodeIgniter\application\models\productmodel.php 
Line: 24 
Function: _error_handler 
+0

似乎你可以使用只需插入無批次 –

+0

我應該只使用插入?! – Ramya

+0

你可能會有陣列嗎?刪除所有'for'循環並且可以使用'insert_batch'。以及'$ data []'改變爲'$ data' –

回答

0

請更改

for ($i = 1; $i < $this->input->post('customer_numrows'); $i++) 
{ 

$data[] = array(
      'product_id' => $temp, 
      'range'  => 0, 
      '[email protected]'  => 0, 
      'price'  => $this->input->post('customer_amount' . $i), 
      'uom'  => $this->input->post('customer_uom' . $i), 
      'usertype' => $customer 
      ); 
} 
$this->db->insert_batch('product_pricing', $data); 
} 

,而你使用$this->db->insert_batch('product_pricing', $data),所有值$data,它必須是數組,其密鑰必須相同