我试图将数据(包括位置信息)从 iOS 模拟器插入到 XAMPP。但我不能。
应用开发环境有 XAMPP(Apache, MySQL)、CakePHP(2.3.0)、Xcode 5。
我可以从 iOS 模拟器插入下一个数据。
NSString *user_name = @"hoge";
NSString *password = @"hogehoge";
NSString *body = [NSString stringWithFormat:@"user_name=%@&password=%@", user_name, password];
但是,我无法插入以下数据。
//iOS side
NSURL *url = [NSURL URLWithString:@"http://macbook.local/locations/index"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
NSString *user_id = @"1";
NSString *lat= @"31.30";
NSString *lng = @"119.70";
NSString *a_b = @"a";
NSString *body = [NSString stringWithFormat:@"user_id=%@&lat=%@&lng=%@&a_b=%@", user_id, lat, lng, a_b];
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!aConnection) {
NSLog(@"connection error.");
}
/* ↑ 我尝试了不同的数据类型(int user_id %d、double lat %f、double lng %f),但结果是一样的。*/
//CakePHP side
public function index(){
if($this->request->is('post') && !empty($this->request->data)) {
//$this->Stamp->create();
$dbo = $this->Stamp->getDataSource();
if(self::isLat($this->request->data['Location']['lat']) && self::isLng($this->request->data['Location']['lat'])){
$this->request->data['Location']['latlng'] = $dbo->expression(
"GeomFromText('POINT({$this->request->data['Location']['lat']} {$this->request->data['Location']['lng']})')"
);
}
$this->Location->save($this->request->data);
}
}
我确认从下面的代码中插入通常的 CakePHP View(index.ctp) 是成功的。
<h1>submit location</h1>
<?php print(
$this->Form->create('Location') .
$this->Form->input('user_id', array('type' => 'text')) .
$this->Form->input('lat') .
$this->Form->input('lng') .
$this->Form->input('a_b') .
$this->Form->end('Submit')
); ?>
最后是 MySQL 表。
//MySQL side
CREATE TABLE locations(
id INTEGER AUTO_INCREMENT,
user_id INTEGER NOT NULL,
latlng GEOMETRY NOT NULL,
created DATETIME,
a_b ENUM('a', 'b'),
PRIMARY KEY(id),
SPATIAL KEY spot_latlng_index(latlng)
) ENGINE = MyISAM;
你觉得哪部分有问题?
请帮助我,给我建议。