【发布时间】:2014-10-28 04:35:43
【问题描述】:
我有以下代码将我的 UITextView 文本转换为编码的 NSString,我可以将其与其他内容一起插入到我的数据库中。插入工作得很好,除非有一个单引号......然后它不起作用。当我 NSLog 编码 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:
<?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);
?>
【问题讨论】:
-
与其从 iOS 应用程序进行验证,为什么不将其移至 PHP?您也应该准备/执行查询。
-
尝试在我的 GET 之后添加它,但它仍然不起作用:$thePost = $mysqli->real_escape_string($thePost);
-
我明白了。谢谢!原来我没有正确更新 php 脚本,所以它甚至没有识别新脚本 XD
标签: php ios mysql objective-c encoding