Search code examples
phpiosobjective-cxcodexcode5

Empty data while trying to send Post method request


I'm trying to send me POST method Request, however when the post is sent the data that is stored in the database is empty i don't get the Value that i have sent this is what i tried:

NSString *queryString = [NSString stringWithFormat:@"http://localhost/json/insertData.php?username=test&password=test"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString: queryString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

All is good, only when the data is sent and i check the database i get en empty values, where is the mistake please.

And this is the code of my PHP file:

<?php

    $host = "localhost";
    $user = "root";
    $pass = "root";
    $db   = "Users";


    $con = mysqli_connect($host,$user,$pass);

    if(!$con)
    {
        die("Can't connect to mysql server!");
    }

    $db_select = mysqli_select_db($con,$db);

    if(!$db_select)
    {
        die("Can't get the selected db!");
    }

    $id       = "11";
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email    = "any1@any1.com";
    $gsm      = "91111111";

    $query = "INSERT INTO Logins VALUES($id,'$username','$password','$email',$gsm)";
    $query_exec = mysqli_query($con,$query);

    if(!$query_exec)
    {
        die("Can't insert data");
    }

?>

Solution

  • Since there is no answer for this question, also you told me that i change the $_POST request to $_GET however as i said i want it post not GET. Finally i got the mistake i have to Encode the DATA using this code:

    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    

    After i added this code, the data has been added successfully.