有人可以发布一些代码,只是将一行数据发布到数据库并读取一行数据回到Xcode中查看?我有一个基本的PHP文件和我一直在玩的Xcode项目,它在我的数据库中更新了一行,但两个字段中的信息都是空白的。
PHP代码减去域名和托管信息:
<?php
$username = "Username";
$pass = "Password";
$database = "test";
mysql_connect('localhost', $username, $pass);
@mysql_select_db($database) or die ("Unable to find database");
$name = @$_POST["name"];
$message = @$_POST["message"];
$query = "INSERT INTO test VALUES ('', '$name', '$message')";
mysql_query($query) or die (mysql_error("error"));
mysql_close();
?>
.h文件
`#import
#define kPostURL @"http://localhost/TESTCONNECT.php" //variable to use whenever
#define kName @"name"
#define kMessage @"message"
@interface FirstViewController : UIViewController{
IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;
}
-(IBAction)post:(id)sender;
@end
.m文件
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) postMessage:(NSString*) message withName:(NSString *) name{
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;}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
如果有人有更好的方法使用Xcode 7.2和swift 2,我想尽可能地进行切换。