iOS,PHP和MySQL - 没有数据写在数据库上

时间:2016-10-30 10:29:54

标签: php ios mysql xcode

我正在创建一个iOS应用程序,它使用PHP文件和MySQL数据库将数据存储到在线数据库。

我设法在iOS应用程序,PHP文件和最终到数据库之间进行通信,但没有写入数据,我只知道通信已经完成,因为每次按下时ID字段都会增加应用程序上的SEND按钮。

我的代码有什么问题?

这是我的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);
?>

2 个答案:

答案 0 :(得分:0)

我不使用我认为是客观的东西-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'];

答案 1 :(得分:0)

$myurl = file_get_contents('https://myurl.html');
$doc = new DOMDocument();
@$doc->loadHTML($myurl);
$divs = $doc->getElementsByTagName('div');

foreach($divs as $div) {
    if ($div->getAttribute('id') === 'j-product-desc') {
        echo "<br/>".$div->nodeValue;
    }
}

因为您使用的是POST方法,所以在php中使用[request setHTTPMethod:@"POST"]; 代替$_POST