This is driving me nuts. It seems so simple, and I am likely missing something obvious - and strongly suspect it's because my PHP/mysql skills are lacking, but I can't get it to work. I have looked hard elsewhere (and stolen) a number of code snippets from StackOverflow trying to solve the issue, but I'm still not convinced I've got it working.

From Xcode I'm trying to encode an NSDictionary object into JSON (using JSON framework) so that I can dynamically store the array in mysql (ideally as a single flattened object - I know, I know) using PHP POST method.

Code is below. I can create the json ok. I can connect ok, I can change variables that arent array variables & that do not need to be send via json, I can do just about anything. I can't seem to pass that json and store it in mysql.

Yes- im a noob.

Thx...

I've got this far:

in xcode

NSDictionary *loginDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"aname", @"username",
                                @"hello", @"password", 
                                nil];   


    NSString *jsonString = [loginDict JSONRepresentation];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    NSString *post = [NSString stringWithFormat:@"json=%@", jsonString];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

    [request setURL:[NSURL URLWithString:@"http://domain.com/post_dict.php"]];
    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:postData];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

at post_dict.php

<?php

$rawJsonData = $_POST['json'];
$decodedData = json_decode($rawJsonData); //do i even need to decode if i want to store a flattened json object in mysql?


//Connect To Database
$hostname='**BLACKEDOUT**.com';
$username='**BLACKEDOUT**';
$password='**BLACKEDOUT**';
$dbname='**BLACKEDOUT**';
$usertable='users';
//I want to update the Records field with the array
$recordsfield = 'Records';


mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);


$query = "UPDATE $usertable SET $recordsfield = '$decodedData' ";//do i encode? serialize? dunno

$result = mysql_query($query);


  if(!$result)
    {
      mysql_close();
      echo mysql_error();
      return;
    }

  mysql_close();


?>
share|improve this question

2 Answers

To answer your comment: //do i even need to decode if i want to store a flattened json object in mysql?

No, you should not json_decode() the data; instead save the $rawJsonData to MySQL. You should however escape it using mysql_real_escape_string(). Here's an example:

$rawJsonData = $_POST['json']; //i don't to decode if i want to store a flattened json object in mysql.

//Connect To Database $hostname='BLACKEDOUT.com'; $username='BLACKEDOUT'; $password='BLACKEDOUT'; $dbname='BLACKEDOUT'; $usertable='users'; //I want to update the Records field with the array $recordsfield = 'Records';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname);

//Escape the JSON data for MySQL
$mysqlEncodedJsonData = mysql_real_escape_string($rawJsonData);

$query = "UPDATE $usertable SET $recordsfield = '$mysqlEncodedJsonData' ";//inserted variable should be mysql_real_escape_string()'d as it is above

$result = mysql_query($query);

if(!$result) { mysql_close(); echo mysql_error(); return; }

mysql_close();
share|improve this answer
1  
awesome - thanks for quick response - ill get on it. – James Jesst Dec 10 '10 at 19:41

If your are sending your JSON in POST method , It can be received in PHP with the below code

<?php $handle = fopen('php://input','r');
                $jsonInput = fgets($handle);
                // Decoding JSON into an Array
                $decoded = json_decode($jsonInput,true);
?>
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.