ios5和JSON:可以从PHP文件中检索数据,但不能发送到数据库


ios5 & JSON: can retrieve data from php file but cannot send to the database

更改了一些东西并得到了一些新的提示。

在我的第一个项目中,我必须从iPad发送一些数据并将其插入到在计算机(MAMP)中运行的SQL数据库中。目前,我尝试仅发送字符串,"城市"和"国家"。其实解决不了两个问题一)下载了 de SBJSON 3,1,将类添加到我的项目中,导入sbjson.h并编写了代码:

在每个NSLog下,我写下了我得到的内容:

#import "SBJson.h"
…
NSArray *keys = [NSArray arrayWithObjects:@"name",@"surname",@"address",@"email",@"city",@"country",@"gender",@"phone",@"age",@"store",@"time", nil];
NSArray *objects = [NSArray arrayWithObjects:name.text,surname.text,address.text,email.text,city.text,country.text,genderLabel,phone.text,age.text,storeCity,lapTime, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding  allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [jsonData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:@"http://192.168.2.111/*****acing/prueba.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSLog(@"jsonData: %@",jsonData);//1
jsonData: <7b227469 6d65223a 2230313a 30373a30 35222c22 61646472 65737322 3a224d61 78696d69 6c69616e 74726173 73652c20 32222c22 6e616d65 223a224f 74746f22 2c226369 7479223a 224dc39c 4e434845 4e222c22 67656e64 6572223a 224d616c 65222c22 70686f6e 65223a22 2b343935 35353232 32323333 222c2265 6d61696c 223a226f 74746f40 64657574 63686c61 6e642e64 65222c22 7375726e 616d6522 3a225072 656d696e 67657222 2c22636f 756e7472 79223a22 4745524d 414e5922 2c226167 65223a22 3630222c 2273746f 7265223a 224dc39c 4e434845 4e227d>

NSLog(@"jsonLength: %d",jsonData.length);//2
223   
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil
                                                    error:nil];
NSString *response = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"response: %@",response);  //3
{"time":"01:07:05","address":"Maximiliantrasse, 2","name":"Otto","city":"MÜNCHEN","gender":"Male","phone":"+49555222233","email":"otto@deutchland.de","surname":"Preminger","country":"GERMANY","age":"60","store":"MÜNCHEN"}

b)我对php一无所知,搜索我在"prueba.php"中写的网络。MySQL连接到php,我可以用表单插入值,但我不知道如何获取数据(如果它到达php文件o.O)并将值插入数据库。

<?php
$hostname_ndb = "localhost";
$database_ndb = "PepeRacing";
$username_ndb = "root";
$password_ndb = "rona5lda";
$ndb = mysql_connect($hostname_ndb, $username_ndb, $password_ndb) or die("Could not connect");
mysql_select_db($database_ndb) or die("Could not connect to the database");
//get file 
$body = @file_get_contents('php://input');
echo $body;
$datosJSON = json_decode($body);
print_r($datosJSON);
$datosJSON=utf8_encode($datosJSON);
mysql_query("INSERT INTO players (city,country,name) VALUES ('{$datosJSON['city']}','{$datosJSON['country']}','{$datosJSON['name']}')");
?>

但是如果我使用以下:

<?php  
if($_POST) {
echo "recibo un paquetito- POST";
//recibo los datos y los descodifico con PHP
$misDatosJSON = json_decode($_POST["jsonData"]);
//debería haber una salida con los datos recibidos, no?
print_r($misDatosJSON);
$salida="";
$salida .="something: " .$misDatosJSON[1];
echo $salida;
}else{
echo "got nothing";
}
?>

输出是"一无所有"

很明显,POST方法不起作用,有什么想法可以解决吗?

感谢您的阅读和帮助!

首先,从PHP文件中删除所有HTML标签,例如,,结束标签等。您正在发送标头,header('Content-Type: text/html; charset=utf-8');,在代码已经发送到浏览器之后。这是不好的编程实践。

此外,您为 PHP 提供的代码只会从数据库中获取数据,而不是插入。

要插入,您需要执行以下操作:

$data_to_insert = json_decode($_POST['json'], true);
mysql_query("INSERT INTO players (city,country) VALUES ('{$data_to_insert['city']}','{$data_to_insert['country']}')");

上面将解码 $_POST json 字符串,分配数组键/值,然后插入到数据库中。