Re: [問題]將資料以POST的方式傳送到PHP的問題
我照著原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,
07/25 15:31
推
07/25 15:41,
07/25 15:41
→
07/25 15:41,
07/25 15:41
→
07/25 19:40,
07/25 19:40
→
07/25 19:42,
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
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
08/16 13:32, 3F
推
08/16 15:36, , 4F
08/16 15:36, 4F
→
08/16 15:51, , 5F
08/16 15:51, 5F
→
08/16 15:54, , 6F
08/16 15:54, 6F
推
08/16 16:52, , 7F
08/16 16:52, 7F
推
08/17 23:30, , 8F
08/17 23:30, 8F
推
08/18 03:20, , 9F
08/18 03:20, 9F
→
08/20 14:15, , 10F
08/20 14:15, 10F
→
08/20 14:15, , 11F
08/20 14:15, 11F
→
08/20 14:21, , 12F
08/20 14:21, 12F
→
08/23 09:30, , 13F
08/23 09:30, 13F
→
08/23 09:30, , 14F
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)
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):