2017-05-25 82 views
-2

我試圖向數據庫中插入一個字符串,但是當它包含單引號(')時,查詢將變爲無效。插入查詢錯誤

$set_content=$_POST['content']; 

$result = pg_query($db,"INSERT INTO programmes(title,picture,content) VALUES('$set_title','$pic_path','$set_content');"); 
+0

不是讓你自己容易受到SQL注入的,用準備好的語句,你不會有這樣的問題:http://php.net/manual/en/function.pg-prepare.php –

+0

你不能在單引號內使用PHP變量。 請嘗試以下查詢 $ set_content = $ _ POST ['content']; $ result = pg_query($ db,「INSERT INTO programs(title,picture,content)VALUES('」。$ set_title。「','。。$ pic_path。」','「。$ set_content。」') ;「); – gyaan

回答

-1

http://php.net/manual/en/function.pg-query.php

警告用戶提供的數據的

字符串插值是極其危險的和 是可能導致SQL注入漏洞。在大多數情況下, 應優先使用pg_query_params(),將用戶提供的值作爲 參數傳遞,而不是將它們替換爲查詢字符串。

$result = pg_query_params($db,"INSERT INTO programmes(title,picture,content) VALUES($1,$2,$3);",Array($set_title,$pic_path,$set_content)); 
+0

非常感謝!它解決了。該查詢現在完美傳遞。 –

+1

我們不應該回答這樣明顯的重複。 –