[英]How To Send “chat message” From iOS TextField To A Remote Database
so I am trying to create a simple iOS program where I can save a chat message to a remote MySQL server and then load it back. 因此,我尝试创建一个简单的iOS程序,在其中可以将聊天消息保存到远程MySQL服务器,然后将其加载回。
My app has textbox where I can enter in data and a button to save the data to an external database. 我的应用程序具有可在其中输入数据的文本框和一个将数据保存到外部数据库的按钮。
My php file looks like this: 我的php文件如下所示:
<?php
$DB_HostName = "hostname";
$DB_Name = "dbname";
$DB_User = "dbuser";
$DB_Pass = "dbpass";
$DB_Table = "dbtable";
if (isset ($_GET["name"]))
$name = $_GET["name"];
else
//$name = "Ghalia";
echo ('Please enter a name');
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$sql = "insert into $DB_Table (name) values('$name');";
$res = mysql_query($sql,$con) or die(mysql_error());
mysql_close($con);
if ($res) {
echo "success";
}else{
echo "faild";
}
?>
My ViewController.h file looks like this: 我的ViewController.h文件如下所示:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize txtName;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)insert:(id)sender {
//create string contains url address for php file, the file name is phpfile.php, it receives a parameter :name
NSString *strURL = [NSString stringWithFormat:@"http://xxdsadxx.net/phpFile.php?name=%@", txtName.text];
//to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
//to receive the returned value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);
}
@end
Right now, I have it coded to only store names. 现在,我已将其编码为仅存储名称。
I want to store a chat message like "hello, how are you?", the string will not store properly to the external server. 我想存储一个聊天消息,例如“你好,你好吗?”,因此该字符串无法正确存储到外部服务器。 Any idea how I can achieve this?
知道我该如何实现吗? Thanks.
谢谢。
You should not use dataWithContentsOfURL. 您不应使用dataWithContentsOfURL。 That is a blocking call.
那是阻塞电话。 It will freeze the UI until the network request complete.
它将冻结UI,直到网络请求完成。 You want to do an HTTP get using NSURLConnection.
您想使用NSURLConnection进行HTTP获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.