2013-04-04 40 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