我尝试使用Xcode,php和mysql将数据发送到在线数据库
创建并测试了mysql数据库和PHP文件。
但是我的IOS代码没有将字符串发送到PHP文件......没有错误,它简单无效!
这里的任何人都知道会出现什么问题吗?
以下是iOS代码:
- (IBAction)send:(id)sender {
NSString *post = [NSString stringWithFormat:@"nome=%@", nome.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://myURLgoes.here/myPHPfile.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postDataLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
这是PHP文件:
<?php
$con=mysqli_connect("MYSITE.COM","USER","PASSWORD","DATABASE");
if (mysqli_connect_errno($con))
{
echo '{"query_result":"ERROR"}';
}
$nome = $_GET['nome'];
$result = mysqli_query($con,"INSERT INTO user (nome)
VALUES ('$nome')");
if($result == true) {
echo '{"query_result":"SUCCESS"}';
}
else{
echo '{"query_result":"FAILURE"}';
}
mysqli_close($con);
?>