我在实际使用 POST 来保存/更改数据库上的信息时遇到了麻烦。有人可以帮我吗?
NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:un, @"username", n, @"name", pn, @"phonenumber",em, @"email", nil];
NSError *error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:kNilOptions error:&error];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
// print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
上面的代码来自应用程序,我的服务器端代码如下所示:另外请不要认为此代码位于 php 文件中
if(!isset($_GET["username"]))
{
// adding new record
$q = sprintf("insert into users (username, name, phoneNumber, email) values ('%s','%s', '%s', '%s')",
mysql_real_escape_string($_GET["username"]),
mysql_real_escape_string($_GET["name"]),
mysql_real_escape_string($_GET["phone"]),
mysql_real_escape_string($_GET["email"]));
}
else
{
// update the record
$q = sprintf("update users set name = '%s', phoneNumber = '%s', email = '%s' where username = '%s'",
mysql_real_escape_string($_GET["name"]),
mysql_real_escape_string($_GET["phoneNumber"]),
mysql_real_escape_string($_GET["email"]),
mysql_real_escape_string($_GET["username"]));
}