[請益] SQL字串中的變數處理,該怎麼做最好

看板PHP作者 (阿銫)時間13年前 (2012/10/13 17:00), 編輯推噓2(205)
留言7則, 4人參與, 最新討論串1/1
當我要執行SQL語法時,會用一個函數將POST進來的變數給包起來 函數長這樣: function GetSQLValueString($theValue, $theType){ $theValue=_addslashes($theValue); //會判斷magic_quotes_gpc再決定使用 //addslashes() switch ($theType) { case "text": //字串 case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long"://常整數 case "int": //整數 $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": //俘點數 $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; } return $theValue; } 所以出來的變數會將單引號(')、雙引號(")前面多加一個反斜線 但是後來GOOGLE才發現它也有漏洞似乎是中文編碼問題,單引號問題並沒有解決 請問這個函數該怎麼去補救? 先說感謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.0.232.61

10/14 02:25, , 1F
可以使用 PDO 或者 mysqli 提供的 func
10/14 02:25, 1F

10/14 02:25, , 2F
10/14 02:25, 2F

10/14 15:22, , 3F
mysql_real_escape_string
10/14 15:22, 3F

10/14 22:07, , 4F
好好的prepared statement不用偏自己硬幹出差不多的東東
10/14 22:07, 4F
因為本來的程式不是用pdo寫 現在轉pdo是個大工程..真沒辦法的話只好改了 ※ 編輯: PTTLSboy 來自: 123.0.232.61 (10/15 18:07)

10/15 22:57, , 5F
OUCH :(
10/15 22:57, 5F

10/16 01:16, , 6F
還蠻想問這段code是哪裡的範例嗎 lol... 怎麼好像看到也有客
10/16 01:16, 6F

10/16 01:17, , 7F
戶的程式碼有看到類似的 lol
10/16 01:17, 7F
這段最原本是dreamweaver產生出來的,會把斜線給去除 加斜線那行是後來補上去的 ※ 編輯: PTTLSboy 來自: 220.128.117.43 (10/16 09:00)
文章代碼(AID): #1GUIsuwi (PHP)
文章代碼(AID): #1GUIsuwi (PHP)