我在网上找到了一个关于如何使用php将数据从ios发布到外部数据库的教程。下面是代码,它可以很好地插入到一个名为test的表中,该表有三列id,name,message。 name是varchar,如果有任何不同,则message是text。
-(void)postMessage:(NSString*) message withName:(NSString *){
if(name !=nil && message !=nil){
NSMutableString *postString = [NSMutableString stringWithString: kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kName , name]];
[postString appendString:[NSString stringWithFormat: @"&%@=%@", kMessage, message]];
[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];
}
}
-(IBAction)post:(id)sender{
[self postMessage:messageText.text withName:nameText.text];
[messageText resignFirstResponder];
messageText.text = nil;
nameText.text = nil;
}
在我的应用程序中,我需要发布两个以上的字段,因此我更改了代码并在表中添加了一个名为test(varchar 30)的额外列。下面是我的新代码,现在不起作用。没有数据输入数据库。为什么这是一个我无法看到的错误?
-(void)postMessage:(NSString*) message withName:(NSString *) name withTestvarchar:(NSString *) testvarchar{
if(name !=nil && message !=nil){
NSMutableString *postString = [NSMutableString stringWithString: kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kName , name]];
[postString appendString:[NSString stringWithFormat: @"&%@=%@", kMessage, message]];
[postString appendString:[NSString stringWithFormat: @"&%@=%@", kTestvarchar, testvarchar]];
[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];
}
}
-(IBAction)post:(id)sender{
[self postMessage:messageText.text withName:nameText.text withTestvarchar:testvarcharText.text];
[messageText resignFirstResponder];
messageText.text = nil;
nameText.text = nil;
testvarcharText.text = nil;
}
我无法理解为什么这不会有任何建议? /你能看到错误吗?
由于