0

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

在我的第一个项目中,我必须从 iPad 发送一些数据并将其插入到 SQL 数据库中,并在我的计算机 (MAMP) 中运行。目前,我试图只发送到字符串“城市”和“国家”。其实解决不了两个问题 a) 下载了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文件oO)并将值插入数据库。

<?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 方法不起作用,有什么解决方法吗?

感谢您的阅读和帮助!

4

1 回答 1

0

对于初学者,请从 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 字符串,分配数组键/值,然后插入数据库。

于 2012-06-06T14:34:41.437 回答