2015-09-07 192 views
1

我正在從批量操作導入.csv文件中的員工數據。有很多領域,如名,姓,電子郵件,聯繫號碼,地址等。PHP腳本中的特殊字符停止執行

數據庫中的電子郵件字段是索引/唯一。所以我已經對它進行了驗證。

但我的問題是,當某些行包含特殊字符,如matiné[email protected]或dé[email protected][焦點]。

這將停止php腳本執行意外沒有錯誤日誌。我也經歷了Apache錯誤日誌/ PHP錯誤日誌,但沒有錯誤。請幫我防止這種情況。

我也嘗試將字符編碼方案設置爲UTF-8或者Westorn-ISO-800等,但這不起作用。

代碼片段:

`     $firstname = $sd[0]; 
        if ($firstname != '' || !empty($firstname)) { 

         $lastname = $sd[1]; 
         $email = $sd[2]; 
         if ($sd[3] != '' && $sd[3] != NULL) {         
          $password = md5($sd[3]); 
         } 
         $contactno = $sd[4]; 
         $internalextension = $sd[5]; 
         $companyid = $this->session->userdata('companyid'); 

         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
          return array("status" => FALSE, "message" => "Error while Importing Employee. Please Check Your .csv " 
           . "file; <br/> Your file contains Invalid Email Id at Line number" . ($count + 1) . ". Email: $email"); 
         } else { 

          if ($count != 500) { 

           $query = $query . "(NULL, '" . $firstname . "', '" . $lastname . "', '" . $email . "', " 
             . "'" . $password . "', '" . $salt . "', 1, '" . $contactno . "'," 
             . "'" . $internalextension . "', 4, " . $companyid . ", '" . my_datenow() . "'),"; 
          } else { 

           $query = $query . "(NULL, '" . $firstname . "', '" . $lastname . "', '" . $email . "', " 
             . "'" . $password . "', '" . $salt . "', 1, '" . $contactno . "'," 
             . "'" . $internalextension . "', 4, " . $companyid . ", '" . my_datenow() . "');"; 
           echo $query; 
          } 
         } 
        } else { 
         return array("status" => FALSE, "message" => "Error while Importing Employee. Please Check Your .csv file; First Name should not be blank."); 
        }` 
+1

還將「排序規則」設置爲「utf8_general_ci」,以便在數據庫中逃脫電子郵件 – Saty

+0

執行數據庫操作之前這一切將立即發生。我的意思是先準備查詢並立即執行。但準備查詢期間發生錯誤。 – Chintan7027

+0

你可以發佈你的準備狀態 – Saty

回答

0

您可以使用分配值的變量,例如

$firstname = htmlspecialchars($sd[0]); 

用htmlspecialchars之前用htmlspecialchars() - 轉換特殊字符爲HTML實體 參考:http://php.net/manual/en/function.htmlspecialchars.php

+0

是的,你是正確的,但只有htmlspecialchars()函數將特殊字符轉換爲html顯示實體,並且將轉換後的電子郵件存儲在數據庫中將會在將來使用電子郵件登錄時產生問題。情況並非如此。 – Chintan7027

+0

我認爲你可以使用mysql_real_escape_string()函數來轉義可能與查詢衝突的字符。例如在創建查詢時使用mysql_real_escape_string($ email)。試一試 – Saurabh

+0

好的,謝謝,讓我試試 – Chintan7027