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.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c