0

我正在尝试将一些值发送到数据库。这是我在 iOS 中的代码和我的 PHP 代码。

我正在后台接收位置,并希望将其存储在我的数据库中,

你能帮忙的话,我会很高兴。谢谢

NSString *latitude =  [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
NSString *longitude =  [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
NSString *stringFromDate = [formatter stringFromDate:newLocation.timestamp];


NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:
 [NSURL URLWithString:@"http://www.yourdomain.com/location.php"]];

[request setHTTPMethod:@"POST"];

NSString *postString = [postString stringByAppendingFormat: latitude, longitude, stringFromDate];

[request setValue:[NSString
                   stringWithFormat:@"%d", [postString length]]
  forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[postString
                      dataUsingEncoding:NSUTF8StringEncoding]];

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




}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
   [httpResponse statusCode];
}

还有我的 PHP 文件

  <?php
  // Connecting, selecting database

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



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

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

 // Performing SQL query
  $query = "INSERT INTO table locatie=(id, longitude, latitude, timestamp)VALUES ('NULL',   '".$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);
 ?>

我在这一行得到 EXC_BAD_ACCES:FIXED

  NSString *postString = [postString stringByAppendingFormat: latitude, longitude, stringFromDate]; **FIXED**

非常感谢你。

问候

4

1 回答 1

1

您的 post 字符串变量的分配发生在评估右侧之后,因此当您在那里使用 postString 变量时,它只是堆栈上的任何垃圾,几乎可以肯定不是指向有效字符串的指针。只需像创建其他字符串一样创建该字符串:

// fix the formatting as necessary for your app
NSString* postString = [NSString stringWithFormat:@"%@ %@ %@", latitude, longitude, stringFromDate];
于 2012-09-03T12:38:49.083 回答