2012-11-01 54 views
-1

將數據(文本和HTML格式)插入到mysql字段LONGTXT中時出現問題。 這裏是錯誤將數據插入到數據庫中:text和html格式

public 'error' => string 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''<p> 

Haec subinde Constantius audiens et quaedam referente Thalassio doc' at line 1' (length=226) 

錯誤和幾乎清楚。我用保護的所有功能(編碼格式,報價,禁用HTML ...) 我想到了另外一個辦法,我創建了兩個函數處理逗號,分號和斜槓 下面是函數和addslashes的代碼:

FUNCTION addText($txt) 
    { 
     IF(get_magic_quotes_gpc()==false) 
       { 
       RETURN utf8_encode(addslashes($txt)); 
       }else{ 
       RETURN utf8_encode($txt); 
       } 

    } 

保護逗號功能:

FUNCTION protect_virgules($txt) 
    { 
     IF($txt!='') 
     { 
      $x = strlen($txt); 
      $newTxt = ''; 
      FOR($i=0;$i<=$x;$i++) 
      { 
        IF($txt[$i]==',' || $txt[$i] == ';') 
        { 

         $newTxt.= '\\'.$txt[$i]; 
        } 
        else 
        { 
         $newTxt.=$txt[$i]; 
        } 
      } 
      RETURN htmlentities($newTxt);    
     } 
     else 
     RETURN '0'; 
    } 

插入PHP函數:

public function insert($table,$data){ 

    $this->last_insert_id = NULL; 

    $fields = ""; 
    $values = ""; 
    foreach($data as $fld => $val){ 
     $values .= trim($this -> escape($val)).","; 
     $fields .= $fld.","; 

    } 
    $values = '"'.substr($values,0,strlen($values)-1).'"'; 
    $fields = '"'.substr($fields,0,strlen($fields)-1).'"'; 
    $tab=array($this->escape($table),$fields,$values,"@last_id"); 
    $this->CALL_SP("sp_insert",$tab); 
    if ($result = mysqli_query($this->linkId, "SELECT @last_id AS last_inserted_id")) { 
     while ($row = mysqli_fetch_assoc($result)) 
     { 
      $this->last_insert_id = $row['last_inserted_id']; 
     } 
    } 

    return $this->queryId; 
} 

插入SQL PROC代碼:

 BEGIN 
SET @stmt_sql=CONCAT("INSERT INTO ", tableName, " (",fields,")VALUES(", param ,")"); 
PREPARE stmt FROM @stmt_sql; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt; 
SELECT LAST_INSERT_ID() into last_id; 
END 

語法錯誤總是抓住我的喉嚨。 你能幫我嗎?

+2

發佈執行的實際SQL查詢。 –

+1

另外,嘗試通過'mysqli_real_escape_string()'傳遞數據。 –

+0

在函數中使用函數escape($ values。= trim($ this - > escape($ val)...使用mysqli_real_escape_string()返回字符串 –

回答

3

不要utf8_encode,除非你想將字符串從Latin-1轉換爲UTF-8。
不要使用addslashes或依靠魔術引號;如果您無法關閉魔術引號或使用stripslashes來撤銷其效果。
不要手動替換和轉義單個字符,除非你有一個非常具體的理由。

一次使用適當的轉義機制數據庫逃生。如果您使用的是mysql_*(不要再使用),請使用mysql_real_escape_string。如果您使用的是mysqli_*,請使用 mysqli_real_escape_string或更好,然後 準備好的語句。如果您使用的是PDO,請使用準備好的語句。

請參閱The Great Escapism (What you need to know to work with text within text)瞭解更多關於該主題的更詳細的討論。

您當前的「準備好的語句」是無用的,因爲它沒有將查詢從值中分離出來。你只是像往常一樣連接所有的值,然後通過一個準備好的聲明強制他們。也不需要存儲過程,這可以使用客戶端API更好地完成。

所以:

  1. 禁用魔術引號as explained in the manual
  2. 使用準備好的語句as explained in the manual
  3. 使用mysqli::insert_id得到最後一個插入ID as explained in the manual
  4. 沒有4.
+0

在PDO的情況下,PDO :: Quote'會很有用嗎? –

+1

是的,'PDO :: quote'是'_real_escape_string'與PDO的等價物,我會***總是建議準備好的語句,而不是轉義,如果你有的話,你需要一個很好的理由來使用任何轉義函數訪問準備的語句。​​ – deceze

+0

第一..感謝您的回覆。 deceze,我會嘗試你的建議。 –