0

我想将用户名和密码发布到我的数据库。在下面的代码中,我从我的用户界面获取我的用户名和密码,这是两个文本字段,一旦我点击登录按钮,它就会调用 URL。但是,不知何故,它没有发布在 SQL 上。另一方面,如果我只是使用我的用户名和密码将 URL 复制并粘贴到浏览器上,它会接受并发布到数据库中。我无法找到我的问题。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize userName;
@synthesize password;
- (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)loginClick:(id)sender {


    NSString *post = [NSString stringWithFormat:@"&userName=%@&Password=%@",userName.text,password.text];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    NSString *str=[NSString stringWithFormat:@"1sikayet.com/getsettt.php?%@", post];
    NSURL *urlbody=[NSURL URLWithString:str];

    NSLog(@"%@",str);

    [request setURL:urlbody];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    [request setHTTPBody:postData];


    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection could not be made");
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    if(theTextField==self.userName)
    {
        [theTextField resignFirstResponder];
    }
    if(theTextField==self.password)
    {
        [theTextField resignFirstResponder];
    }
    return YES;
}
@end

getettt.php

        $hostname = "sikayet.db.XXXX.com";
        $username = "sikayet";
        $dbname = "sikayet";

        //These variable values need to be changed by you before deploying
        $password = "Hidden"
        $usertable = "sikayetUp";
         ?>

        //Connecting to your database
        mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
        connect to database! Please try again later.");
        mysql_select_db($dbname);

        //Fetching from your database table.
        $query = "INSERT into $usertable values('null','".$userName."','".$login."')";
        $result = mysql_query($query);
        echo $userName.",".$login;
         ?>
4

3 回答 3

1

请使用以下代码,这可以帮助您将数据发布到服务器

  NSString *postString = [NSString stringWithFormat:@"http://www.1sikayet.com/getsettt.php?&userName=%@&Password=%@",userName.text,password.text];
               NSURL* url = [NSURL URLWithString:postString];
                NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:url];

                NSURLConnection* connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if(connection)
        {
            NSLog(@"Connection Successful");
        }
        else
        {
            NSLog(@"Connection could not be made");
        }
于 2013-07-03T04:32:22.090 回答
1

如果您可以通过浏览器 url 发送数据,那么您正在使用方法 get 并且您的 html 正文看起来与此类似

<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

但是对于您在 IOS 代码中使用的发布方法,您的网站应该遵循与此类似的内容

<body>
<form action="<?php $_PHP_SELF ?>" method="POST">

Name: <input type="text" name="name" />
Age: <input type="text" name="age" />

<input type="submit" />
</form>

这些示例取自http://www.tutorialspoint.com/php/php_get_post.htm,显示了“post”和“get”之间的区别,您应该使用“post”来登录和保护信息,但如果您想更改您的ios代码现在可以工作看起来像删除以请求开头的代码行只会连接到url/发送数据,就像你在网页中键入它时说它有效并且它有效一样。

如果您使用 $_REQUEST,您也可以在服务器端接收 post 和 get 方法。

PHP $_REQUEST 变量包含 $_GET、$_POST 和 $_COOKIE 的内容。

于 2013-07-02T21:53:30.897 回答
0

以下代码完美运行。我最初忘记输入“http://”

- (IBAction)loginClick:(id)sender {
        // Create the request.


        NSString *post = [NSString stringWithFormat:@"userName=%@&Password=%@",userName.text,password.text];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

        //NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        NSString *str=[NSString stringWithFormat:@"http://1sikayet.com/getsettt.php?%@", post];
        NSURL *urlbody=[NSURL URLWithString:str];

        NSLog(@"%@",str);

        NSURLRequest *request = [NSURLRequest requestWithURL:urlbody];


        NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
        if(conn)
        {
            NSLog(@"Connection Successful");
        }
        else
        {
            NSLog(@"Connection could not be made");
        }


    }
于 2013-07-02T22:29:09.993 回答