通过PHP从iOS文本字段向mySQL数据库添加新数据


Adding new data to mySQL database through PHP from iOS textfield

这些东西仍然很难找到,但现在开始了。。我正在尝试将一个新的播放器(用户在iOS中的文本字段中输入)添加到mySQL数据库中。我知道这里已经有类似的问题了,但我已经尝试了所有的方法,仍然无法解决!

以下是Xcode中的方法:

-(IBAction)Register:(id)sender {
    NSString *newPlayer = [NSString stringWithFormat:@"PlayerName=%@", _inputPlayerName.text];
    NSData *data = [newPlayer dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://domainName.com/NewPlayer.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:nil];
    NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"responseData: %@", returnString);
}

和PHP:

<?php
// Create connection
$con=mysqli_connect("localhost","UserName","Password","DatabaseName");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO TableName (PlayerName) VALUES('$_POST[PlayerName]')";
mysql_query($con,$sql);
// Close connections
mysqli_close($con);
?>

NSLog中的responseData返回为空白。如果有人能看到我哪里出了问题,我将不胜感激!有代表或我需要分配的任何东西吗?

我现在似乎已经修复了它。这个问题存在于我使用的变量中(我有大约20个lol)。错误的语法也潜伏着。。