简体   繁体   中英

iOS inserting JSON to MySQL

I'm trying insert data to MySQL with JSON format but it is not working. The data is not insert to database. Could you tell what I'm doing wrong? This is my PHP file:

<?php

$con = mysql_connect("http://mydomain", "dbUserName", "password");
if(!$con)
{
    die('Connect fail', mysql_error());
}

mysql_select_db("dbName", $con);

if(isset($_REQUEST['data']))
{
     $json = $_REQUEST['data'];
     $data = json_decode($json);

     mysql_query("INSERT INTO tbAccount 
                         (AccFullName, AccAboutMe) 
                  VALUES ('$data->AccFullName', '$data->AccAboutMe')");
}
$con->exec($mysql_query);
echo "New record created successfully";

mysql_close($con);?>

and implementation .m in app in obj-c:

- (IBAction)insert:(id)sender;{NSMutableString *postString = [NSMutableString stringWithString:kPostURL];

NSString *jsonString = [[NSString alloc] initWithFormat:@"{\"AccFullName\":\"%@\", \"AccAboutMe\":\"%@\"}", self.name.text, aboutMe.text];

[postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]];

[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];

postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

NSLog(@"Post string %@", postString);}

Encode Base64 your data and then try to submit to MySql, Maybe you have any specific characters.

NSString *plainString = @"Any text ..... bla bla bla";

NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM