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:@"https://xxxxxxx.com/signup.php"]]];
[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:
<?php
$con=mysqli_connect("SERVER","USER","PASS","DATABASE");
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"}';
}
else{
echo '{"query_result":"FAILURE"}';
}
mysqli_close($con);
?>