【问题标题】:Codeigniter - insert batch - my sqlCodeigniter - 插入批处理 - mysql
【发布时间】:2017-04-01 00:44:54
【问题描述】:
 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 ),
          'vat@'       => $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 ),
          'vat@'       => $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。

  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

【问题讨论】:

  • 看来你可以只使用 Insert without batch
  • 我应该只使用插入吗?!
  • 你有多少个数组?删除所有for 循环并可以使用insert_batch。以及$data[] 更改为$data
  • 但我必须在我的表中的“用户类型”列中插入第一批的“批发”和第二批的“经销商”和第三批的“客户”
  • 使用 3 个 diff 数组,例如 $usertype$wholesale$dealer ...

标签: mysql codeigniter


【解决方案1】:

这个错误的特殊之处在于:

您的 SQL 语法有错误;查看与您的 MySQL 服务器版本相对应的手册,了解在第 1 行使用 near 'Array' 的正确语法

INSERT INTO `my_table` () VALUES (1,2,3), Array

这意味着 , Array 实际上被放入查询中......我知道这没有多大意义,但我认为这是一个 CodeIgniter 错误,原因如下:

  1. 在调用my_tableinsert_batch 之前,我有不止一个insert_batch 调用其他表,并且它们都运行良好
  2. my_table 的列比之前所有获得 insert_batches 的表多得多;
  3. 调用my_tableinsert_batch 仅在参数数组中使用非空 ['column_name' =&gt; 'value'](每行只有1 列)有效...所以看起来数量/类型表结构所具有的参数正在影响此方法的功能。

所以为了确认这是一个 CodeIgniter 问题/错误(与表的结构和 insert_batch 方法有关),我添加了单独的插入。例如:

$this->db->insert('my_table', ['col1' => 'value']);
$this->db->insert('my_table', ['col1' => 'value', 'col2' => 'value']);

他们使用我提供给insert_batch 数组数组的完全相同的列/值对。

【讨论】:

    【解决方案2】:

    请修改

    for ($i = 1; $i < $this->input->post( 'customer_numrows' ); $i++)
    {
    
     $data[] = array(
              'product_id' => $temp,
              'range'       => 0,
              'vat@'       => 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-&gt;db-&gt;insert_batch( 'product_pricing', $data ) 时,对于 $data 中的所有值,它必须是数组,其键必须相同

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 2016-03-19
      相关资源
      最近更新 更多