dqby43944 2017-05-06 02:40
浏览 55
已采纳

如何在CI中将多维数组插入数据库?

I have this array.

Array
(
[customer_id] => Array
    (
        [customer_id] => 132
    )

[file_name] => Array
    (
        [0] => ht5appletv11.png
        [1] => twitter-100px11.png
    )

[file_path] => Array
    (
        [0] => C:/xampp/htdocs/maintenance_assurance_sys/uploads/ht5appletv11.png
        [1] => C:/xampp/htdocs/myproject/uploads/twitter-100px11.png
    )

)

I want to insert these data into database with Row by Row in CI.But the customer_id is always same in each row. file_name and file_path will change. For example:

How can i do that?

+---------------------------------------------------------------------+
|  customer_id     | file_name                |file_path              |
|   132            |  ht5appletv11.png        | C:/xampp/htodcs/..... |
|   132            |  twitter-100px11.png     | C:/xampp/htodcs/..... |
+---------------------------------------------------------------------+

Here is my Code...

function insertUploadedFiles($data)
{   

    foreach ($data as $fileData)
    {   
        $datas[] = array(
            'customer_id' => $fileData['customer_id'],
            'file_name' => $fileData['file_name'],
            'file_path' => $fileData['file_path']
        );

    }
    $this->db->insert_batch('t_customer_file_ref', $datas);
}

But it's only get customer_id and the file_name & file_path show error like this.

Message: Undefined index: file_name

Undefined index: file_path

  • 写回答

1条回答 默认 最新

  • douna6802 2017-05-06 03:00
    关注

    Please try the below code,

    function insertUploadedFiles($data)
    {   
    
    foreach ($data['file_name'] as $key => $fileData)
    {   
          $datas[] = array(
              'customer_id' => $data['customer_id']['customer_id'],
              'file_name' => $fileData,
              'file_path' => $data['file_path'][$key]
          );
    
      }
      $this->db->insert_batch('t_customer_file_ref', $datas);
    }
    

    I assume that both the file_name and file_path elements will have same indexes like 0, 1, 2 etc.,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同