我正在制作一个“发布文本”应用程序并尝试使用名称和正文字段更新我的数据库。obj-c 代码如下所示:
- (IBAction)post:(id)sender
{
NSLog(@"%@", name);
NSLog(@"%@", body);
// create string contains url address for php file, the file name is post.php, it receives parameter :name
NSString *strURL = [NSString stringWithFormat:@"http://www.mywebsite.com/post.php?name=%@&body=%@",name, body];
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);
}
php 代码如下所示:
<?php
if (isset($_REQUEST['name']) && isset($_REQUEST['body']) && strlen($_REQUEST['name']) > 0 && strlen($_REQUEST['body']) > 0) {
//get data
$title = $_REQUEST['name'];
$body = $_REQUEST['body'];
mysql_connect("MyServer","username","password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$date = date("Y-m-d");
//insert data
$insert = mysql_query("INSERT INTO news VALUES ('','".mysql_real_escape_string($title)."','".mysql_real_escape_string($body)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
die("Your text has been posted!");
}
?>
有时它可以通过应用程序发布,但它只是将 (null) 作为名称发布,将 null 作为正文发布。但在大多数情况下,它根本不起作用。它只是在日志中说:
2012-06-26 16:54:49.893 AppName[12444:707]
<UITextField: 0x17a390; frame = (63 76; 194 31); text = 'MyName';
clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17a4c0>>