duandaotuo5542 2014-11-02 23:37
浏览 64

在Objective C中将数据发送到服务器

I am currently trying to send data from my xcode project to a server that I had made but the data doesn't seem to appear in my tables when I run my application.

It is quite simple, allowing the user to enter a name and a message which is sent when a button is pressed. I do not know what I've done wrong, any suggestions?

Here is my php code:

<?php

$username = "root";
$database = "testdb";

mysql_connect('localhost', $username);

@mysql_select_db($database) or die ("Unable to find database");

$name = @$_GET["name"];
$message = @$_GET["message"];

$query = "INSERT INTO test VALUES ('', '$name', '$message')";

mysql_query($query) or die (mysql_error("error"));

mysql_close();

?> 

and my obj c code:

.h

#import <UIKit/UIKit.h>

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

2条回答 默认 最新

  • douwenan9849 2014-11-03 00:04
    关注

    You are doing a post in objetive C, so in PHP you need to catch the variables with $_POST, not $_GET.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用