dongxian2863 2012-06-26 15:56
浏览 45
已采纳

通过objective-c更新MySQL

I'm making a 'post text' app and trying to update my database with name and body field. The obj-c code looks like this:

- (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);
}

And the php code looks like this:

<?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!");
} 


?>

Sometimes it works to post through the app but then it just post (null) as name and null as body. But for the most part it doesn't work at all. It just says in the log:

    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>>
  • 写回答

1条回答 默认 最新

  • dqpfkzu360216 2012-06-26 16:02
    关注

    Taking a wild guess here but are name and body pointers to a UITextField object? I'm thinking they might be, based on your log output. If so, you need to use [name text] or name.text to get the actual NSString value from the text field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R