[問題]將資料以POST的方式傳送到PHP的問題

看板MacDev作者 (樂)時間13年前 (2012/07/25 14:48), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串1/2 (看更多)
我想用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

07/25 15:31, , 1F
我猜 [NSString stringWithFormat:@"jason=%@" <==
07/25 15:31, 1F

07/25 15:41, , 2F
NSString *jsonString = [jsonDictionary JSONRepresent
07/25 15:41, 2F

07/25 15:41, , 3F
可以這樣做? 需要匯入甚麼東西嗎?
07/25 15:41, 3F

07/25 19:40, , 4F
謝謝!!可以了!!
07/25 19:40, 4F

07/25 19:42, , 5F
回2F 好像要去匯入SBJSON喔!!
07/25 19:42, 5F
文章代碼(AID): #1G3vQxjt (MacDev)
文章代碼(AID): #1G3vQxjt (MacDev)