0

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

?>
SniperCoder
  • 823
  • 2
  • 11
  • 23

4 Answers4

0

You are passing the variables username and password via GET. In your PHP script you have to use $_GET instead of $_POST:

$username = $_GET['username'];
$password = $_GET['password'];
mario.van.zadel
  • 2,919
  • 14
  • 23
0

in your Error url shows

http://localhost/json/insertData.php?username=test&password=test

you can clearly see username=test&password=test have all data. It means your form method="get",

So in your code

$username = $_GET['username'];
$password = $_GET['password'];

This will solve your problem.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

More specifically, in the form from the previous page, you should change the submitting form tag to include method="post".

<form method="post">
Tyler Collins
  • 127
  • 10
0

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.

SniperCoder
  • 823
  • 2
  • 11
  • 23