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

标签 php ios mysql objective-c post

仍然是这些东西的菜鸟,但现在开始...... 我正在尝试将一个新播放器(用户在 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 个哈哈)。不正确的语法也潜伏着..

关于php - 通过 PHP 从 iOS 文本字段向 mySQL 数据库添加新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28554170/

相关文章:

php - 使用 Slim 框架的 REST API 客户端 key

iphone - Storyboard设备特定资源

ios - 如何让从 Storyboard 中实例化的 UIViewCojntroller 嵌入到 UINavigationController 中

php - Mysql 仅使用 date_sub 中的时间或替代方案

javascript - 如何在网页加载时自动提交表单?

php - 严格标准 : only variables should be passed by reference - why does this still work and how can I correct it?

ios - 为什么 API 拒绝我的 JSON?

mysql - 在数据库记录/Mysql中搜索字母

mysql - 使用 Sqoop 创建/导入 Hive 表

php - 使用 PHP 5.6 生成范围内的安全随机整数