【问题标题】:ios5 & JSON: can retrieve data from php file but cannot send to the databaseios5 & JSON:可以从 php 文件中检索数据但不能发送到数据库
【发布时间】:2012-06-06 14:08:53
【问题描述】:

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

在我的第一个项目中,我必须从 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文件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方法不起作用,有什么解决办法吗?

感谢您的阅读和帮助!

【问题讨论】:

  • 我认为将所有代码塞进一个按钮是不明智的:),它会阻塞你的主线程。让我们简单点,您不要将数据直接发布到 Mysql DB,而是向 php 发出请求,将其添加到 DB。我没有看到任何 GET 变量,您是否在发出请求?
  • 我的意图是在给定时刻发送保存的数据(核心数据)。这是我的第一个应用程序,希望确定应用程序连接并发送数据。有没有办法在没有 php 文件的情况下将值插入数据库?谢谢
  • objective-c 和 php 的变化。

标签: php objective-c json ios5 database-connection


【解决方案1】:

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

【讨论】:

  • 是的,php 代码获取数据,是我唯一知道的 :)。感谢您提供的代码和编程技巧。
  • 拦截连接:请求URL:localhost/../prueba.php请求方法:GET
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 2018-09-19
  • 2017-09-07
  • 2018-10-13
相关资源
最近更新 更多