2013-04-04 71 views
0

我有一些代码上传CSV文件到指定的文件夹,但它不会更新数据库。Codeigniter CSV上传然后爆炸

public function do_upload() 
{ 
    $csv_path = realpath(APPPATH . '/../assets/uploads/CSV/'); 
    $config['upload_path'] = $csv_path; 
    $config['allowed_types'] = '*'; // All types of files allowed 
    $config['overwrite']  = true; // Overwrites the existing file 

    $this->upload->initialize($config); 
    $this->load->library('upload', $config); 

    if (! $this->upload->do_upload('userfile')) 
    { 
     $error = array('error' => $this->upload->display_errors()); 

     $this->layout->buffer('content', 'program/upload', $error); 
     $this->layout->render(); 
    } 
    else 
    { 


     $image_data = $this->upload->data(); 
     $fname = $image_data['file_name']; 
     $fpath = $image_data['file_path'].$fname; 
     $fh = fopen($fpath, "r"); 



     $insert_str = 'INSERT INTO wc_program (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n"; 


     if ($fh) { 

      // Create each set of values. 
      while (($csv_row = fgetcsv($fh, 2000, ',')) !== false) { 

       foreach ($csv_row as &$row) { 
        $row = strtr($row, array("'" => "\'", '"' => '\"')); 
       } 

       $insert_str .= '("' 
        // Implode the array and fix pesky apostrophes. 
        .implode('","', $csv_row) 
        .'"),'."\n"; 
      } 

      // Remove the trailing comma. 
      $insert_str = rtrim($insert_str, ",\n"); 

      // Insert all of the values at once. 
      $this->db->set($insert_str); 

      echo '<script type="text/javascript"> 
         alert("Document successfully uploaded and saved to the database."); 
         location = "program/index"; 
       </script>'; 
     } 
     else { 
       echo '<script type="text/javascript"> 
         alert("Sorry! Something went wrong please proceed to try again."); 
         location = "program/upload"; 
       </script>'; 
     } 


    } 
} 

当我运行var_dump($ fh);它显示:类型(流)的资源(89)

当我运行var_dump($fpath)它表明:string(66) "/Applications/MAMP/htdocs/site/assets/uploads/CSV/wc_program.csv"

所以所有上传操作,但什么是错了,没有更新的数据库? 我试过各种改变fopen方法,但仍然没有喜悦,我真的需要它来添加到数据库和插入查询和设置查询应该做的伎俩,但它没有。

任何帮助非常感谢!

+0

你有'db_debug'在'config/database.php'中设置为'TRUE'吗?它会显示数据库错误。您也可以尝试回显'$ this-> db-> last_query()'来查看它运行的最后一个查询。 – 2013-04-04 20:08:49

回答

1

您没有在数据库上运行任何查询。您正在使用简单的查询语法混合活动记录语法。活动记录插入查询将通过调用执行。

$this->db->insert('my_table'); 

db::set()实际上没有查询数据库。它需要在调用db::insert()db::update()之后插入或更新的键/值对。如果您自己构建查询,则需要使用db::query()函数。

查看active directory documentation

您可以使用$this->db->query('put your query here'),但您失去了CodeIgniter内置安全性的好处。查看CodeIgniter's query功能。

我给你举几个例子,你可以使用CodeIgniter插入数据库。这些示例将从您的评论中生成查询。您将需要相应地调整您的代码。

实施例1

$result = $this->db 
    ->set('JobRef', 911847) 
    ->set('Area', 'Coastal') 
    ->set('Parish', 'Yapton') 
    ->set('AbbrWorkType', 'Micro') 
    ->set('WorkType', 'Micro-Asphalt Surfacing') 
    ->set('Timing', 'TBC') 
    ->set('TrafficManagement', 'No Positive Traffic Management') 
    ->set('Location', 'Canal Road (added PMI 16/07/12)') 
    ->set('Duration', '2 days') 
    ->set('Start', '0000-00-00') 
    ->set('Finish', '0000-00-00') 
    ->insert('wc_program'); 

echo $this->db->last_query() . "\n\n"; 
echo "RESULT: \n\n"; 
print_r($result); 

实施例2(使用的关联数组):

实施例1和2所使用的活动记录。信息将逐个存储,然后在进行最终调用时构建查询。这有几个优点。它允许您动态构建查询,而不必担心SQL语法和关键字的顺序。它也逃避你的数据。

例3(简单查询):

$query = 'INSERT INTO 
    wc_program 
     (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) 
    VALUES 
     ("911847","Coastal","Yapton","Micro","Micro-Asphalt Surfacing","TBC","No Positive Traffic Management","Canal Road (added PMI 16/07/12)","2 days","0000-00-00","0000-00-00")'; 
$result = $this->db->query($query); 
echo $this->db->last_query() . "\n\n"; 
echo "RESULT: \n"; 
print_r($result); 

这样离开对所有注射保护你的,可能会导致更多的错误,并且是难以改变/维持。

如果你打算这样做,你应该使用下面的语法,这将防止注入。

实施例4

$query = 'INSERT INTO 
    wc_program 
     (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) 
    VALUES 
     (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);'; 

$row = array(
    911847, 
    'Coastal', 
    'Yapton', 
    'Micro', 
    'Micro-Asphalt Surfacing', 
    'TBC', 
    'No Positive Traffic Management', 
    'Canal Road (added PMI 16/07/12)', 
    '2 days', 
    '0000-00-00', 
    '0000-00-00' 
); 

$result = $this->db->query($query, $row); 
echo $this->db->last_query() . "\n\n"; 
echo "RESULT: \n"; 
print_r($result); 

笨将取代每个 “?”在查询中使用数组中相应的值进行转义。您可以使用它来运行许多具有相同形式但具有不同数据的查询,只需更新$row阵列并从CI的内置安全性中受益即可。

+0

$ this-> db-> query()抛出并返回错误,说我需要使用set和$ this-> db-> insert()也声明您必须使用“set”方法来更新条目。 – 2013-04-04 20:43:58

+0

因此,要澄清它上传文件,但实际上并未插入/更新数据库,就像它不读取CSV或查询不正确。 – 2013-04-04 20:45:31

+0

var_dump在最后一次查询之后做了一个直接的处理:string(269504)“INSERT INTO wc_program(JobRef,Area,Parish,AbbrWorkType,WorkType,Timing,TrafficManagement,Location,Duration,Start,Finish)VALUES(”911847“ ,“沿海”,“亚普顿”,“微型”,“微型沥青路面”,“TBC”,“没有正面交通管理”,“Canal Road(新增PMI 16/07/12)”,“2天” “0000-00-00”,“0000-00-00 ...等等等等等等等等等等等等等等等,但是插入查询有 – 2013-04-04 20:50:04