我使用此方法向服务器发送post请求并将数据保存到sql数据库但我遇到了一个问题,当数据保存到数据库时,我漏掉了一些字符例如,我有一个字符串"examplepost++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');");
Remember+在URL中表示空格。如果你想传递一个+符号,那么你必须对它进行编码,我认为你的xcode是通过UTF8格式来实现的。在POST中,请求参数作为请求正文中的查询字符串。因此,调试你的PHP代码,并确保一旦你的PHP接收到数据,仍然将其保存在UTF8中。如果不是,那么简单地使用php中的urlencode(string$str)来转换它。我认为你的问题在于编码,你需要调试你的代码,以准确地识别哪个部分没有正确编码。问题好,祝你好运