可能重复:
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