作者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