duandang2838
2015-03-22 14:04
浏览 75
已采纳

Codeigniter多维数组插入数据库

Hello im trying to insert a multidimensional array into mysql using codeigniter. Im having some trouble getting the values to enter correctly. Here is how the multidimensional array looks

Array ( [2] => Array ( [A] => FName 2 [B] => LName 2 [C] => PhoneNo 2 [D] => FaxNo 2 ) [3] => Array ( [A] => FName 3 [B] => LName 3 [C] => PhoneNo 3 [D] => FaxNo 3 ) [4] => Array ( [A] => FName 4 [B] => LName 4 [C] => PhoneNo 4 [D] => FaxNo 4 ) [5] => Array ( [A] => FName 5 [B] => LName 5 [C] => PhoneNo 5 [D] => FaxNo 5 ) [6] => Array ( [A] => FName 6 [B] => LName 6 [C] => PhoneNo 6 [D] => FaxNo 6 ) [7] => Array ( [A] => FName 7 [B] => LName 7 [C] => PhoneNo 7 [D] => FaxNo 7 ) [8] => Array ( [A] => FName 8 [B] => LName 8 [C] => PhoneNo 8 [D] => FaxNo 8 ) [9] => Array ( [A] => FName 9 [B] => LName 9 [C] => PhoneNo 9 [D] => FaxNo 9 ) ) 

Here is what i've tried doing

function insertfiles($arr_data)
{
    foreach ($arr_data as $value) {
        foreach($value as $key => $a){
            $data = array(
                   'Firstname' => $a,
                   'Lastname' => $a,
                   'Phone'=>$a,
                   'Fax' =>$a
                );

                $this->db->insert('test', $data); 
        }
    }
}

Im sure im messing this up horribly, it is inserting the values into the database but it's inserting them as follows

since it wont let me upload an image im providing a link to view this image online. http://psadatadesign.com/img/test-bmp.jpg

any help would be appreciated. As im trying to figure out what i have done wrong.

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

您好我试图使用codeigniter将多维数组插入到mysql中。 我无法正确输入值。 以下是多维数组的外观

 数组([2] =>数组([A] => FName)  2 [B] => LName 2 [C] => PhoneNo 2 [D] => FaxNo 2)[3] =>数组([A] => FName 3 [B] => LName 3  [C] => PhoneNo 3 [D] => FaxNo 3)[4] =>数组([A] => FName 4 [B] => LName 4 [C] => PhoneNo 4 [  D] => FaxNo 4)[5] =>数组([A] => FName 5 [B] => LName 5 [C] => PhoneNo 5 [D] => FaxNo 5)[  6] =>数组([A] => FName 6 [B] => LName 6 [C] => PhoneNo 6 [D] => FaxNo 6)[7] =>数组([A]  ] => FName 7 [B] => LName 7 [C] => PhoneNo 7 [D] => FaxNo 7)[8] =>数组([A] => FName 8 [B]  => LName 8 [C] => PhoneNo 8 [D] => FaxNo 8)[9] =>数组([A] => FName 9 [B] => LName 9 [C] =  > PhoneNo 9 [D] => FaxNo 9))
   
 
 

以下是我尝试过的事情

   function insertfiles($ arr_data)
 {
 n 到达($ arr_data为$ value){
 foreach($ value as $ key =>  $ a){
 $ data = array(
'名字'=> $ a,
'姓氏'=> $ a,
'电话'=> $ a,
'传真'=  > $ a 
); 
 
 $ this-> db-> insert('test',$ data);  
} 
} 
} 
   
 
 

我确定我搞砸了这个,它将数值插入到数据库中但是它按如下方式插入

因为它不会让我上传图片我提供了一个在线查看此图片的链接。 http://psadatadesign.com/img/test-bmp.jpg

任何帮助将不胜感激。 正如我试图弄清楚我做错了什么。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dsxcv5652 2015-03-22 16:00
    最佳回答

    Use this rather your own function

    function insertfiles($arr_data)
    {
        foreach($array_data as $a){
            $data = array(
                   'Firstname' => $a['A'],
                   'Lastname' => $a['B'],
                   'Phone'=>$a['C'],
                   'Fax' =>$a['D']
                );
    
                $this->db->insert('test', $data); 
        }
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题