我正在创建一个 iOS 应用程序,它使用 PHP 文件和 MySQL 数据库将数据存储到在线数据库。
我设法在 iOS 应用程序、PHP 文件和数据库之间进行通信,但是上面没有写入任何数据,我只知道通信已经完成,因为每次按下时 ID 字段都会递增应用程序上的“发送”按钮。
我的代码有什么问题?
这是我的 iOS 代码:
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);
和 PHP 文件:
<?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);
?>
最佳答案
我不使用我认为是 objective-c 的东西,所以我不完全确定你的第一段代码,但是,你似乎在发布数据,然后在 PHP 中你试图获取来自全局 $_GET 变量的数据。要调试这个,请尝试用
替换您设置变量的位置$nome = 'Nome';
$endereco = 'Endereco';
$telefone = 'Telefone';
$email = 'Email';
$mensagem = 'Mensagem';
$coordenadas = 'Coordenadas';
这将让您查看它是否正确存储数据,因为它应该存储上述字符串而不是尝试从全局 $_GET 变量中获取。如果这可行,请尝试用等效的 $_POST 变量替换变量:
$nome = $_POST['nome'];
$endereco = $_POST['endereco'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$mensagem = $_POST['mensagem'];
$coordenadas = $_POST['coordenadas'];
关于php - iOS、PHP 和 MySQL - 没有数据写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40328117/