Php 为什么我的单引号(';)没有被编码?

Php 为什么我的单引号(';)没有被编码?,php,ios,mysql,objective-c,encoding,Php,Ios,Mysql,Objective C,Encoding,我有以下代码将我的UITextView文本转换为编码的NSString,我可以将其与其他内容一起插入数据库。插入的效果非常好,除非只有一个引号。。。那就不行了。当INSLog编码的NSString时,甚至没有将'。因此,当它执行web服务器请求时,url中仍有“导致其失败的原因…”。。。为什么单引号没有被优先编码?下面是我的代码(我对php也不是很在行): iOS: NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCre

我有以下代码将我的UITextView文本转换为编码的NSString,我可以将其与其他内容一起插入数据库。插入的效果非常好,除非只有一个引号。。。那就不行了。当INSLog编码的NSString时,甚至没有将'。因此,当它执行web服务器请求时,url中仍有“导致其失败的原因…”。。。为什么单引号没有被优先编码?下面是我的代码(我对php也不是很在行):

iOS:

NSString *encodedString = (NSString 
*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
  NULL, (CFStringRef)self.textView.text,     NULL,  (CFStringRef)@"!*'();:@&=+$,/?%#[]",      kCFStringEncodingUTF8 ));          
  NSString *strURL = [NSString stringWithFormat:@"http://example.org/postText.php?thePost=%@&byUserID=%@&nickname=%@", encodedString, [UniqueUserIdentification getUserID], nickname];
<?php

$con=mysqli_connect("editout","editout","editout","editout");    
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$thePost = $_GET["thePost"];
$byUserID = $_GET["byUserID"];
$nickname = $_GET["nickname"];

mysqli_query($con,"INSERT INTO MyTable
VALUES ('$thePost', '$byUserID', '$nickname', 0, 0)");    
mysqli_close($con);

?>
php:

NSString *encodedString = (NSString 
*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
  NULL, (CFStringRef)self.textView.text,     NULL,  (CFStringRef)@"!*'();:@&=+$,/?%#[]",      kCFStringEncodingUTF8 ));          
  NSString *strURL = [NSString stringWithFormat:@"http://example.org/postText.php?thePost=%@&byUserID=%@&nickname=%@", encodedString, [UniqueUserIdentification getUserID], nickname];
<?php

$con=mysqli_connect("editout","editout","editout","editout");    
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$thePost = $_GET["thePost"];
$byUserID = $_GET["byUserID"];
$nickname = $_GET["nickname"];

mysqli_query($con,"INSERT INTO MyTable
VALUES ('$thePost', '$byUserID', '$nickname', 0, 0)");    
mysqli_close($con);

?>

在客户端中,它的代码为%27,这是正确的。但是在服务器中,您的sql使用错误。

您的sql是错误的

$thePost = $_GET["thePost"];
您使用GET数据分配
$thePost
,但这是url解码

例如,
$post
是“
我们走吧!
”,因此您的SQL变成:

INSERT INTO MyTable
VALUES ('Let's go', '$byUserID', '$nickname', 0, 0)"
显然存在语法错误

你需要用它来转义字符串


看看。

与其从iOS应用程序进行验证,为什么不将其移动到PHP?您也应该准备/执行查询。请注意,在我的GET之后尝试添加此项,但仍然不起作用:$thePost=$mysqli->real\u escape\u string($thePost);我得到了它。非常感谢。原来我没有正确更新php脚本,所以它甚至没有识别出新脚本XDurlencode只适用于url。对于其他领域,这将造成混乱局面。参数化查询是一种方法。url不是他插入的唯一字段。你看到他的代码了吗,他也对参数进行了编码。使用编码结果发布到服务器。服务器代码错误。^并且他在服务器中收到了错误。在sql中,它有一些表示字符串引号的含义。因此服务器可以获取值,但代码是:mysqli_query($con,“INSERT-INTO-MyTable-VALUES(“$thePost'”、“$byUserID'、“$昵称”,0,0)”;是错误的。请打印该sql,它可能喜欢将此插入MyTable值(“fdsaf”、“32”、“fdsa”、“0,0”),它的值中有“multiply”,因此数据库将显示错误。它应该在sql字符串中转义“multiply”。