0

JSONStringで4つの異なる値を送信します。どういうわけか(デコード?)これらをPHP値に変換して、MySQLデータベースに送信する必要があります。

この関数はphpファイルに送信します:

- (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);

}

そして、それはおそらくこの部分でうまくいかないでしょう:

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

これは私のログです:

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

ID、経度、緯度、タイムスタンプをMySQL挿入用に準備する必要があるこのPHPファイルに送信します

 <?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);
?>
4

2 に答える 2

1

このコードを試してください:

$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);
于 2012-10-09T14:51:15.147 に答える
0

単純なタイプミス。交換

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

NSString* jsonString = [[NSString alloc] initWithFormat:@"\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"", _phonenumber, longitude , latitude, stringFromDate];
于 2012-10-09T14:27:11.617 に答える