[問題]將資料以POST的方式傳送到PHP的問題
我想用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
07/25 15:31, 1F
推
07/25 15:41, , 2F
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
07/25 19:42, 5F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):