【发布时间】:2013-12-08 20:03:07
【问题描述】:
我使用此方法向我的服务器发送 post 请求并将数据保存到 sql 数据库 但是我有一个问题,当数据保存到数据库时我错过了一些字符 例如我有这个字符串“example post ++ request ++” 当接收到数据库字符串时,通过缺少 + 加号更改“示例发布请求” 这是我的代码
xcode
NSString *STR = @"mystring=example post ++ request ++";
NSData *PostData = [STR dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://MySiteUrl.com/example.php"]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:PostData];
[request setHTTPMethod:@"POST"];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
php代码
$STR = $_POST['mystring'];
header ('Content-Type: text/html; charset=UTF-8');
$con = mysql_connect("localhost","xxxxxx","xxxx");
mysql_select_db("xxxxxxx", $con);
mysql_query("SET NAMES utf8");
$STR = trim($STR);
mysql_query("INSERT INTO `xxxxxxx`.`table` (`id`, `mystring`) VALUES (NULL, '$STR');");
【问题讨论】:
-
警告:您使用的是a deprecated database API,应该使用modern replacement。您也容易受到SQL injection attacks的影响,现代 API 可以让您更轻松地从 defend 中获得。
标签: php post nsurlconnection httprequest nsurlrequest