1

i use this method to send post request to my server and save data to sql database but I've a problem i miss some character when data saved to database for example i have this string "example post ++ request ++" when received to database string changes "example post request" by missing + Plus signs here is my code

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 code

$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');");
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Fawaz
  • 584
  • 1
  • 11
  • 24
  • 1
    **Warning:** you're using [a **deprecated** database API](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) yourself from. – Marcel Korpel Dec 08 '13 at 20:21

1 Answers1

1

Remember +, in a URL, means a space. If you want to pass a + symbol though then you have to encode it which I think your xcode is doing it via UTF8 format. In POST the request parameters are taken as query string in the request body. So debug your PHP code and make sure once your PHP receives that data then still keeps it in UTF8. If it's not then simply use the urlencode ( string $str ) in php to convert it. I think your problem is with encoding and you need to debug your code to exactly identify which part is not getting encode properly. Good question and good luck

grepit
  • 21,260
  • 6
  • 105
  • 81