view raw
Vladimir Vladimir - 6 months ago 44
iOS Question

iOS, PHP and MySQL - No data is written on database

I'm creating an iOS App that stores data to an online database using a PHP file and MySQL database.

I managed to make the communication between the iOS app, the PHP file and finally to the database, but no data is written on it, I only know that the communication has been done because the ID field is incremented every time I press the SEND button on the App.

What can be wrong with my code?

Here is my iOS code:

NSString *post = [NSString stringWithFormat:@"nome=%@&endereco=%@&telefone=%@&email=%@&mensagem=%@&coordenadas=%@", nome.text, adressLabel.text, telefone.text, email.text, mensagem.text, coordenadas.text];
const char *urlUTF8 = [post UTF8String];
NSString *postBody = [NSString stringWithUTF8String:urlUTF8];

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

NSString *postDataLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@""]]];
[request setHTTPMethod:@"POST"];
[request setValue:postDataLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"****** RESPOSTA: %@", content);

And the PHP file:

if (mysqli_connect_errno($con))
echo '{"query_result":"ERROR"}';

$nome = $_GET['nome'];
$endereco = $_GET['endereco'];
$telefone = $_GET['telefone'];
$email = $_GET['email'];
$mensagem = $_GET['mensagem'];
$coordenadas = $_GET['coordenadas'];

$result = mysqli_query($con,"INSERT INTO user (nome, endereco, telefone, email, mensagem, coordenadas)
VALUES ('$nome', '$endereco', '$telefone', '$email', '$mensagem', '$coordenadas')");

if($result == true) {
echo '{"query_result":"SUCCESS"}';
echo '{"query_result":"FAILURE"}';


I don't work with what I believe to be objective-c so I'm not entirely sure about your first block of code, however, you do appear to be posting data, then in PHP you're attempting to grab data from the global $_GET variable. To debug this try replacing where you're setting your variables with

$nome = 'Nome';
$endereco = 'Endereco';
$telefone = 'Telefone';
$email = 'Email';
$mensagem = 'Mensagem';
$coordenadas = 'Coordenadas';

This will let you see if it is storing data correctly as it should store the above strings instead of trying to grab the from the global $_GET variable. If this works try replacing the variables with the $_POST variable equivalent:

$nome = $_POST['nome'];
$endereco = $_POST['endereco'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$mensagem = $_POST['mensagem'];
$coordenadas = $_POST['coordenadas'];