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

看板MacDev作者 (老董)時間11年前 (2012/08/15 23:49), 編輯推噓7(707)
留言14則, 6人參與, 最新討論串2/2 (看更多)
我照著原po的code去做改寫 目的是希望能改到資料庫中的name欄位中的值 不過試了很久 還是卡在上傳料庫時 NSLog的部分是有讀到東西的 但是資料庫接收到的資料都是空的 想起問各位大大有沒有方法... xcode部分 NSURL *objUrl = [NSURL URLWithString :@"http://xxxxxxx.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:@"json=%@", jsonString]; NSLog(@"%@",strHttpBody); [theRequest setHTTPBody:[strHttpBody dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[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? $result = mysql_query($query); ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.47.134.69

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

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

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

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

07/25 19:42,
回2F 好像要去匯入SBJSON喔!!
07/25 19:42
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.121.55

08/16 09:02, , 1F
........ 我還以為我的推文變成控制碼了..
08/16 09:02, 1F

08/16 09:05, , 2F
那你的NSURLConnection的delegate部分勒?
08/16 09:05, 2F
痾 可不可以請ten大說簡單一點舉個例子之類的 我還是新手XD 如果是didReceiveResponse那些function我有做了 確定有跑到 didReceiveRespons 內 也有匯入 SBJSON了 BTW 其實我一直在想說 是不是php端的 $rawJsonData = $_POST['json']; <--這行有問題 post內要打其他甚麼之類的? ※ 編輯: shes050117 來自: 140.113.121.55 (08/16 10:17)

08/16 13:32, , 3F
我不是大大... PHP部份我都不知道...
08/16 13:32, 3F

08/16 15:36, , 4F
php端要用$_GET
08/16 15:36, 4F

08/16 15:51, , 5F
用$_GET['json']; 這樣?
08/16 15:51, 5F

08/16 15:54, , 6F
還是不行 @@
08/16 15:54, 6F

08/16 16:52, , 7F
搞錯了 Sorry
08/16 16:52, 7F

08/17 23:30, , 8F
Name = $decodedData->{'Name'}
08/17 23:30, 8F

08/18 03:20, , 9F
php var_dump($_POST)有東西嗎
08/18 03:20, 9F

08/20 14:15, , 10F
你要先弄個什麼東西可以看server return回來的東西
08/20 14:15, 10F

08/20 14:15, , 11F
先確認你送出了什麼東西 以及server有沒有收到...
08/20 14:15, 11F

08/20 14:21, , 12F
08/20 14:21, 12F

08/23 09:30, , 13F
h大F大的方法我都是過了 還是不行@@ 可能是我一些東西
08/23 09:30, 13F

08/23 09:30, , 14F
沒用到 最後我是用 $_REQUEST 就OK嘞
08/23 09:30, 14F
我最後改了方法 直接用URL傳東西 XCODE 改成這樣 NSMutableString *postString =[NSMutableString stringWithString:@"http://XXX.XXXX.XXX.XXX.php"]; NSString *jsonString =[[NSString alloc] initWithFormat:@"{\"Name\":\"%@\",\"Action\":\"%@\",\"Start\":\"%@\",\"End\":\"%@\"}",postN,postA,postS,postE]; NSLog(jsonString); [postString appendString:[NSString stringWithFormat:@"?data=%@",jsonString]]; [postString setString:[postString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; [request setHTTPMethod:@"POST"]; postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; NSLog(@"post string %@",postString); php 改成這樣 if(isset($_REQUEST['data'])) { $json = $_REQUEST['data']; $data = json_decode($json); } ※ 編輯: shes050117 來自: 140.113.121.55 (08/23 09:49)
文章代碼(AID): #1GAyK55O (MacDev)
文章代碼(AID): #1GAyK55O (MacDev)