0

I send 4 different values in a JSONString. I need to somehow (decode?) convert these to PHP values to send it to a MySQL database.

This function sends is to the php file:

- (void)myFuntionThatWritesToDatabaseInBackgroundWithLatitude:(NSString *)latitude longitude:(NSString *)longitude date:(NSString *)stringFromDate 
{
    _phonenumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];

    NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
    NSString*jsonString = [[NSString alloc] initWithFormat:@{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}", _phonenumber, longitude , latitude, stringFromDate];

    [postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]];
    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString ]];
    [request setHTTPMethod:@"POST"];

    [[NSURLConnection alloc] initWithRequest:request delegate:self ];
    NSLog(@"Post String =%@", postString);

    //    LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init];
    //    phonenumber = locationTestViewController.telefoonnummer;
    NSLog(@"HERE1 : %@", _phonenumber);

}

And it probably goes wrong in this part:

NSString* jsonString = [[NSString alloc] initWithFormat:@{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}", _phonenumber, longitude , latitude, stringFromDate];

This is my log:

2012-10-09 15:32:59.869 MyApp[626:c07] Post String=http://www.yourdomain.com/locatie.php?  data=%7B%22id%22:%220612833397%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2209-10%2015:05%22%7D

It sends it to this PHP file where the id, longitude, latitude and timestamp needs to be made ready for the MySQL insert

 <?php

    $id = $_POST['id'];
    $longitude = $_POST['longitude'];
    $latitude = $_POST['latitude'];
    $timestamp = $_POST['stringFromDate'];

    $link = mysql_connect('server', 'bla', 'bla') or die('Could not connect: ' . mysql_error());

    mysql_select_db('bla') or die('Could not select database');

    // Performing SQL query
    $query="INSERT INTO locatie (id, longitude, latitude, timestamp) 
    VALUES ($id, $longitude,$latitude,$timestamp)";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    echo "OK";

    // Free resultset
    mysql_free_result($result);

    // Closing connection
    mysql_close($link);
?>
1
  • On a side note, please use mysql_real_escape_string. Inserting unescaped data into queries is pretty dangerous! Commented Oct 9, 2012 at 14:57

2 Answers 2

1

Try this code:

$tmpdata = urldecode($_GET['data']);
$data = json_decode($tmpdata);
$id = $data['id'];
$longitude = $data['longitude'];
$latitude = $data['latitude'];
$timestamp = $data['stringFromDate'];

$link = mysql_connect('server', 'bla', 'bla') or die('Could not connect')

mysql_select_db('bla') or die('Could not select database');

// Performing SQL query
$query="INSERT INTO locatie (id, longitude, latitude, timestamp) 
VALUES ($id, $longitude,$latitude,$timestamp)";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
Sign up to request clarification or add additional context in comments.

3 Comments

Didnt work either, I still get the message that there are no values in $id, $longitude, $latitude and $timestamp
That't least because json_decode creates stdClass instances by default, ie you'd need to access properties like this $id = $data->id;, or use json_decode($tmpdata, true); in order to decode into an associative array.
and add "true" to json_decode($tmpdata, true); as ndm suggested :)
0

Simple typo. Replace

NSString* jsonString = [[NSString alloc] initWithFormat:@{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}", _phonenumber, longitude , latitude, stringFromDate];

with

NSString* jsonString = [[NSString alloc] initWithFormat:@"\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"", _phonenumber, longitude , latitude, stringFromDate];

3 Comments

Doesnt this needs to be removed too? "}"
Wouldn't that produce an invalid JSON string? They are ment to be enclosed in {}, aren't they?
If you use json_decode, like alan978 suggested, then yes and you should put the parentheses back. Not like in original code. There you missed a single opening ".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.