可能重複:
apostrophes are breaking my mysql query in PHP撇號不會在MySQL/PHP工作
我的Android應用程序在發送郵件到我的服務器(PHP腳本) 的PHP腳本編寫消息到MySQL。
它工作正常,但所有包含撇號的消息(例如他要去學校)都不會寫入數據庫。
這裏的PHP腳本:
function deq($s)
{
if($s == null)
return null;
return
get_magic_quotes_gpc() ?
stripslashes($s) : $s;
}
if(md5(deq($_REQUEST['blub']).deq($_REQUEST['blub'])."blabla") == $_REQUEST['hash']){
mysql_connect("localhost", "blub", "blub") or die(mysql_error());
mysql_select_db("blub") or die(mysql_error());
// mysql_set_charset('UTF8'); // does not work as too old php
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
$mysqldate = gmdate('Y-m-d H:i:s');
$language = (int) $_REQUEST['language'];
$device = $_REQUEST['device'];
$ipaddress = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO test(username, text, language, rating, checked, date)
VALUES('".$_REQUEST['username']."', '".$_REQUEST['text']."', '".$language."', '0' , 'false', '".$mysqldate."') ")
or die(mysql_error());
mysql_close();
閱讀:[大逃避現實(或:你需要知道什麼工作用文本中的文本)](http://kunststube.net/escapism/) – deceze
您正在使用[一個過時的數據庫API](http://stackoverflow.com/q/12859942/19068),應該使用[現代替換](http: //php.net/manual/en/mysqlinfo.api.choosing.php)。你也暴露了自己[SQL注入攻擊](http://bobby-tables.com/),一個現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-防止 - 注入 - 在PHP中)自己從。 – Quentin