作者chichi30 (乐)
看板MacDev
标题[问题]将资料以POST的方式传送到PHP的问题
时间Wed Jul 25 14:48:24 2012
我想用POST的方式将资料以json格式传送到php,接受後decode json
然後UPDATE mysql...
但是不管我怎麽试就是没动静
看了很多网页教学也都一样找不到问题..
希望有人可以帮我找到问题,拜托了!!
这是我的程式码
NSURL *objUrl = [NSURL URLWithString :
@"
http://localhost/liors~/postdict.php"] ;
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL :
objUrl] ;
[theRequest setHTTPMethod : @"POST"];
[theRequest setValue : @"application/json"
forHTTPHeaderField : @"Content-Type"];
[theRequest setTimeoutInterval:10];
NSString *stringData = [[NSString alloc] initWithString:@"BOSS"];
NSDictionary *jsonDictionary = [NSDictionary
dictionaryWithObject:stringData forKey:@"Name"];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSString *strHttpBody = [NSString stringWithFormat:@"%@", jsonString];
[theRequest setHTTPBody:[strHttpBody
dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
php的部分
<?php
$rawJsonData = $_POST['json'];
$decodedData = json_decode($rawJsonData);
//Connect To Database
$hostname='**BLACKEDOUT**.com';
$username='**BLACKEDOUT**';
$password='**BLACKEDOUT**';
$dbname='**BLACKEDOUT**';
$usertable='users';
mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to
database! Please try again later.');
mysql_select_db($dbname);
$query = "UPDATE $usertable SET Name = '$decodedData' ";//do i encode?
serialize? dunno
$result = mysql_query($query);
?>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.47.134.69
1F:推 LouisXIV:我猜 [NSString stringWithFormat:@"jason=%@" <== 07/25 15:31
2F:推 tentenlee:NSString *jsonString = [jsonDictionary JSONRepresent 07/25 15:41
3F:→ tentenlee:可以这样做? 需要汇入甚麽东西吗? 07/25 15:41
4F:→ chichi30:谢谢!!可以了!! 07/25 19:40
5F:→ chichi30:回2F 好像要去汇入SBJSON喔!! 07/25 19:42