1
我使用此方法發送post請求到我的服務器並將數據保存到sql數據庫 但我有一個問題,我錯過了一些字符時,數據保存到數據庫 例如我有這個字符串「例如交++請求++」 通過缺失+加號收到數據庫字符串修改 「例如POST請求」 當 這裏是我的代碼發送http發佈請求時發生錯誤
的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 **棄用的**數據庫API](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql -phunctions-in-php)並且應該使用[現代替換](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com)**,現代的API會更容易[防守](http://stackoverflow.com/questions/60174/)我怎麼可以防止SQL注入在PHP)自己從。 –