0

I would like to post userName and password to my database. In the following code I am getting my userName and password from my user interface which are two textFields and once I click on login button, and it suppose to call the URL. However, somehow it is not posting on SQL. On the other side, if I just copy and paste the URL on browser with my userName and Password, it accepts and post on database. I could not able to find my problem.

#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

getsettt.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
  • 1
    if you can paste or write the url in the into your browser and it works then isn't that a php get "which is not good for login" and here you are doing a php "post" Commented Jul 2, 2013 at 21:29
  • what should I do? What should it be? What is the best way to send data from iphone to server? Commented Jul 2, 2013 at 21:38
  • Is this your php code on the server or someone else's / a website that is not yours? Commented Jul 2, 2013 at 21:41
  • website is mine, php code is on server side. Please see updated code above with my php script Commented Jul 2, 2013 at 21:43

3 Answers 3

1

If you can send data through a browser url then you are using method get and your body of your html would look something similir to this

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

but for the method of posting you are using in your IOS code your website should follow something similir to this

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

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

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

these examples are taken from http://www.tutorialspoint.com/php/php_get_post.htm showing the difference between "post" and "get" and you should use "post" for logins and secure information but if you wanted to change your ios code to work right now it looks like removing the lines of code that start with request would simply connect to the url/send the data just as you do when you say it works when you type it in a webpage and it works.

as well you could receive post and get methods on server side if you use $_REQUEST.

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

Sign up to request clarification or add additional context in comments.

Comments

1

Please use the following code this helps you post data to server

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

2 Comments

somehow, it is not getting!
i think you missing the task that is function in php
0

The following code works perfectly. I initially forgot to put "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");
        }


    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.