上传到mysql服务器无法正常工作

时间:2014-04-17 15:32:34

标签: 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();

?>

EDIT !!! 现在,这会在数据库中输入一个新条目,但由于NSString,所有字段都是空白的,而在数据库中,列是整数。然而,当我改变PHP到这个

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

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

3 个答案:

答案 0 :(得分:3)

发现问题:

xcode的:

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

PHP:

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

除此之外,您还容易受到SQL injection attacks

的攻击

答案 1 :(得分:0)

 <?php
include 'connection.php';

$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];

$query = "INSERT INTO Register (username,password,firstname,lastname,email) VALUES   ('$username', '$password', '$firstname', '$lastname', '$email')";

$ result = mysqli_query($ connection,$ query);

echo $query;

我看起来像这样并且没有错误地工作

答案 2 :(得分:0)

S [postString appendString:[NSString stringWithFormat:@“?%@ =%@”,kHalfid,halfid]];有个 ?而不是&amp;。