【问题标题】:codeigniter array values insert product_idcodeigniter 数组值插入 product_id
【发布时间】:2018-01-11 09:13:10
【问题描述】:

我正在尝试使用codeigniter 在数据库中插入array。但是我遇到了类似的错误

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '0, 1, 2, 3) VALUES ('5', '6', '11', '13')' 附近使用正确的语法

插入customer_orders(0、1、2、3)值('5'、'6'、'11'、'13')

文件名:C:/xampp/htdocs/hari/billing-system/system/database/DB_driver.php

行号:691

控制器:

class Customer_order extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('orders');
    }
    
    function addnewcustomerorder()
    {
        $product_id = $this->input->post('product_id');
        $data = array(
            'product_id' => $product_id
            );
        //print_r($data);
        $res = $this->orders->addnewcustomerorder($data);
    }

型号:

function addnewcustomerorder($data)
{
    if($data['product_id']!="") 
    {
        foreach($data as $a)
        {
            $res=$this->db->insert('customer_orders',$a);
            return $this->db->insert_id();
        }
    }
    else
    {
        return false;
    }
}

我想像这样在database 中插入值

注意:忽略customer_id

我如何insert 数据库中的值

【问题讨论】:

    标签: arrays codeigniter insert


    【解决方案1】:

    这有助于命名变量它们是什么,因为 $data['product_id'] 是一个 id 数组,它应该是复数 ($data['product_ids']),这将有助于调试您的代码

     //since product_id is an array of product ids (should change it to $data['product_ids']) 
    /*$data = [
            'product_id' => [2,3,4,6,7]
            ];  
    */
    //var_dump($data); showing values like array(1) { ["product_id"]=> array(4) { [0]=> string(1) "5" [1]=> string(1) "6" [2]=> string(2) "11" [3]=> string(2) "13" } } i pass only product_id 
    function addnewcustomerorder($data)
    {
      $orders = [];
      foreach($data['product_id'] as $product_id){
    
         if($product_id AND $product_id != "") 
          {
    
             $this->db->insert('customer_orders',['product_id'=>$product_id]);
             $orders[] =  $this->db->insert_id();
    
         }
      }
      return $orders;
    
    
    }
    

    【讨论】:

    • 感谢您的回答兄弟显示错误Error Number: 1054 Unknown column 'Array' in 'field list' INSERT INTO customer_orders` (product_id) 值(数组)文件名:C:/xampp/htdocs/hari/billing-system/system/database/DB_driver。 php行号:691`
    • var_dump($data);显示像 array(1) { ["product_id"]=> array(4) { [0]=> string(1) "5" [1]=> string(1) "6" [2]=> string(2) "11" [3]=> string(2) "13" } } 这样的值,我只通过 product_id
    • 你需要做 foreach($data['product_id'] as product_id)
    • 检查我添加的内容。
    • 天哪,又是错误INSERT INTO customer_orders` (5) VALUES ('')`
    【解决方案2】:

    首先,在数据库查询中使用 forech 不是建议性的。 Codeigniter 有insert_batch 函数来向数据库添加多行。你应该使用它。

    现在来解决您的问题,如果这是您要插入的数组,请参考您的评论:

    Array
    (
        [product_id] => Array
            (
                [0] => 1
                [1] => 3
                [2] => 4
                [3] => 5
            )
    
    )
    

    那么这是可以帮助你的代码。

    你的控制器:

    function addnewcustomerorder()
    {
        $product_id = $this->input->post('product_id');        
        $res = $this->orders->addnewcustomerorder($product_id);
    }   
    

    你的模特:

    function addnewcustomerorder($product_id)
    {
        if($data['product_id']!="") 
        {
            $your_product_array = ''; 
    
            foreach($product_id as $pro_id)
            {
                $your_product_array[] = array('product_id'=>$pro_id);                       
            }
            $this->db->insert_batch('customer_orders',$your_product_array);
            // This above code inser all the row at once. 
            // I am suggesting to use database transaction.
            return true;
        }
        else
        {
            return false;
        }
    }
    

    供参考:这里是your_product_array的数组结构:

    Array
    (
        [0] => Array
            (
                [product_id] => 1
            )
    
        [1] => Array
            (
                [product_id] => 3
            )
    
        [2] => Array
            (
                [product_id] => 4
            )
    
        [3] => Array
            (
                [product_id] => 5
            )
    
    )
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      把你的模型改成这个 假设您的 order_id 是自动递增的,而 customer_id 将为空或 null(因为您不需要发送该值)

      function addnewcustomerorder($data){
          if($data['product_id']!="") {
              foreach($data as $a){
                  $res=$this->db->insert('customer_orders', array('product_id' => $a));
                  return $this->db->insert_id();
              }
          }
          else{
              return false;
          }
      }
      

      【讨论】:

      • 谢谢 抱歉显示错误Unknown column 'Array' in 'field list' INSERT INTO customer_orders` (product_id) VALUES (Array)`
      • 好的,设置为$data = array('customer_id' => NULL, 'product_id' => $a); $this->db->insert('customer_orders', $data);
      • 你的意思是在foreach
      • 您不需要 foreach 循环,因为您要插入一个数据行
      • 兄弟再次错误错误号:1054“字段列表”中的未知列“数组”插入到customer_orderscustomer_idproduct_id)值(NULL,数组)文件名:C:/ xampp/htdocs/hari/billing-system/system/database/DB_driver.php 行号:691
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      相关资源
      最近更新 更多