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

如何在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

图片转代码服务由CSDN问答提供 功能建议

我有这个数组。

  Array 
(\  n [customer_id] =>数组
(
 [customer_id] => 132 
)
 
 [file_name] =>数组
(
 [0] => ht5appletv11.png 
  [1] => twitter-100px11.png 
)
 
 [file_path] =>数组
(
 [0] => C:/xampp/htdocs/maintenance_assurance_sys/uploads/ht5appletv11.png  
 [1] => C:/xampp/htdocs/myproject/uploads/twitter-100px11.png 
)
 
)
   
 
 

I 想要在CI中逐行插入这些数据到数据库中。但是每行的customer_id总是相同的。 file_name和file_path将更改。 例如:

我该怎么做?

  + ---  --------------------------------------------------  ---------------- + \ N |  customer_id |  file_name | file_path | 
 |  132 |  ht5appletv11.png |  C:/ xampp / htodcs / ..... | 
 |  132 |  twitter-100px11.png |  C:/ xampp / htodcs / ..... | 
 + -----------------------------------  ---------------------------------- + 
   
 
 <  p>这是我的代码...  
 
 
  function insertUploadedFiles($ data)
 {
 
 foreach($ data as $ fileData)
 {
 $  datas [] = array(
'customer_id'=&gt; $ fileData ['customer_id'],
'file_name'=&gt; $ fileData ['file_name'],
'file_path'=&gt; $ fileData ['  file_path'] 
); 
 
} 
 $ this-&gt; db-&gt; insert_batch('t_customer_file_ref',$ datas); 
} 
   
 
  

但它只获得customer_id和file_name&amp; file_path显示如下错误。

消息:未定义的索引:file_name

未定义的索引:file_path < / blockquote>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.,

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题