您好,在我的应用程序中,我将数据存储在sqlite3数据库中,并将我作为json传递到我的服务器端脚本,我已将代码插入在线服务器中我使用php脚本。一切正常,但在我的在线服务器中存储空数据。
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field1Str] forKey:@"name"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field2Str] forKey:@"city"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field3Str] forKey:@"phone"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field4Str] forKey:@"email"];
[array addObject:_dataDictionary];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@"http://indianpoliticalleadersmap.com/IOS/trail/insert.php"];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
// print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
}
当我记录json时,它显示为这种格式。
[{"名称":" sdfsdf""电话":" 0""城市&#34 ;: " DSF""电子邮件":" DSF"}] 2014-03-19 20:06:21.567数据库[2576:70b] JSON摘要:[{" name":" sdfsdf"," phone":&# 34; 0""城市":" DSF""电子邮件":" DSF"},{"名称& #34;:" sdfsf""电话":" 13123123""城市":" 13123123",& #34;电子邮件":" sdfsdfdsfsd"}]
如果我打印出我已经存储数据字典的数组,它就像显示的那样。
[array addObject:_dataDictionary];
{
city = "selva kumar";
email = madurai;
name = 8870882627;
phone = "shantharsk@gmail.com";
},
如果我在上面的代码字段1Str,field2Str .....
中打印各个字段名:Aswin,城市:karnanataka,电话:133213232,邮箱:VVVVVV
这是我的PHP代码,用于插入我的在线数据库。
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
//echo $json;
//Database Connection
require_once 'db.php';
/* insert data into DB */
mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email)
VALUES ('".$obj->{'name'}."', '".$obj->{'phone'}."', '".$obj->{'city'}."', '".$obj->{'email'}."')");
//database connection close
mysql_close($con);
//}
?>
请告诉我在上面这段代码中的位置,为什么它不存储在我的数据库中请给出一些解决方案我已经堆积了这么长时间我不能弄清楚它吃了我的脑袋
感谢。
答案 0 :(得分:0)
您是否尝试var_dump($json);
和var_dump($obj);
?在插入数据库之前?
看来你发送的数组有一个对象,所以试试下面的内容:
$obj[0]->name; // etc ...
$ obj将包含一个数组,如果json字符串包含一个带有对象的数组,要在下面的注释中回答你的问题,你可以在下面找到如何声明一个数组:
$myArray = array('a','b');
或如下所示的关联数组:
$myArray = array('a' => 'a value', 'b' => 'b value');
回到你的具体案例:
如果你在php服务器端有这个json字符串:
[
{"name":"sdfsdf","phone":"0","city":"dsf","email":"dsf"},
{"name":"sdfsf","phone":"13123123","city":"13123123","email":"sdfsdfdsfsd"}
]
你需要一个循环来抛出数组中的所有对象并插入它们,如下所示:
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
//var_dump($json); // you can use var_dump for debug.
//Database Connection
require_once 'db.php';
/* insert data into DB */
foreach ($obj as $element){
mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email)
VALUES ('".$element->name."', '".$element->phone."', '".$element->city."', '".$element->email."')");
}
//database connection close
mysql_close($con);
//}
?>