php - 上传到mysql服务器不工作

标签 php ios sql objective-c

我需要将数据添加到我的 mysql 数据库中。但是,实际数据库中没有添加任何内容,下面是我的 xcode,下面是我的 php 代码。似乎不明白为什么它不起作用?有什么建议/你能看到错误吗?

-(void) postMessage:(NSString*) teamid withPlayerid:(NSString *) playerid withFixtureid:(NSString *) fixtureid withEventid:(NSString *) eventid withHalfid:(NSString *) halfid{

//check isnt receiving two anit paramters
if(teamid !=nil && playerid !=nil && fixtureid !=nil && eventid !=nil && halfid !=nil){

    NSMutableString *postString = [NSMutableString stringWithString:kPostURL];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@",kTeamid, teamid]];
    //makes kname equal to name
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kPlayerid , playerid]];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@",kFixtureid, fixtureid]];
    //makes kname equal to name
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kEventid , eventid]];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@",kHalfid, halfid]];
    //makes kname equal to name

    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    NSLog(@"%@",teamid);
    NSLog(@"%@",KTeamid);


}
}

-(IBAction)prop1Button:(id)sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"1 - Prop"];

 _textField3.text = myString;
[self postMessage: self.textField2.text withPlayerid:self.textField3.text withFixtureid:self.textField3.text withEventid:self.textField3.text withHalfid:self.textField3.text];
_textField2.text = nil;
_textField3.text = nil;
dropGoalCounter ++;
[[NSNotificationCenter defaultCenter] postNotificationName:@"DoUpdateLabel" object:nil userInfo:nil];
[self dismissViewControllerAnimated:YES completion:nil];


}

这是我的用于插入数据库的 php 代码

<?php
include ("./inc/connect.inc.php");
$teamid = (int)$_POST["teamid"];
$playerid = (int)$_POST["playerid"];
$fixtureid = (int)$_POST["fixtureid"];
$eventid = (int)$_POST["eventid"];
$half = $_POST["halfid"];

$query = "INSERT INTO MatchEvent (MatchEventID, TeamID, PlayerID,FixtureID,EventID,Half) VALUES ('','9','1','8','7','$half')";

mysql_query($query) or die(mysql_error("error"));

mysql_close();

?>

编辑! 现在,这将在数据库中输入一个新条目,但假设由于 NSString 而所有字段都是空白,并且在数据库中列是整数。然而当我将 php 更改为这个时

$teamid = (int)$_POST["teamid"]
$playerid = (int)$_POST["playerid"];
$fixtureid = (int)$_POST["fixtureid"];
$eventid = (int)$_POST["eventid"];
$half = (int)$_POST["halfid"];

它根本不向数据库添加条目。有什么建议吗?

最佳答案

发现问题:

x代码:

[request setHTTPMethod:@"POST"];
                         ^^^^^

PHP:

$teamid = $_GET["teamid"];
            ^^^----hmmmmm. not $_POST

除此之外,您还很容易受到 SQL injection attacks 的攻击。

关于php - 上传到mysql服务器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23142326/

相关文章:

javascript - HTML 表单不会捕获 Bootstrap 响应表中的所有复选框

javascript - 源代码和 DOM 有什么区别?

iphone - 将图像添加到自定义单元格 subview

ios - Xcode Profile和物理设备离线问题

c# - 如何根据值列表选择行

php - Codeigniter 如何在当前 Controller 中获取所有/特定方法

php - 访问mysql数据库为网站提取多个数据的最有效方法

php - 在窗口中启动 Ajax 脚本 - 窗口关闭时停止

MySQL 服务器表创建 - "invalid syntax"

java - Arraylist 中的子列表与 SQL 中的 limit