Please, help me to solve one problem. I have wasted a lot of time, but still cant find what`s wrong.
A have a mySql database on web named "c3chickens". It has 1 table "chickens" and contains 3 rows: id, nickname and score. My application connects to database, reads and parses data and show the result. It works fine.
But I also need to write new nicknames and scores in database columns. Here is code for this operation in xcode:
NSDictionary *jsonElementToServer = [[NSDictionary alloc] initWithObjectsAndKeys:login, @"login", bestScore, @"score", nil];
NSLog(@"score %@", [jsonElementToServer description]);
NSError* err = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonElementToServer options:0 error:&err];
NSString *url = @"http://chickens.gol.com/php.php";
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0f];
NSMutableData *body = [NSMutableData data];
[body appendData:jsonData];
[theRequest setHTTPBody:body];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
if (connection) {
NSLog(@"connect");
self.infoData = [NSMutableData data];
} else {
NSLog(@"Connection failed");
}
And here is my php.php file:
<?php
$username = "...";
$password = "...";
$hostname = "localhost";
$dbnamemysql = "c3chickens";
$mysqli = new mysqli($hostname, $username, $password, $dbnamemysql);
if(!$mysqli){
exit("ERROR DB");
$result = "NO METHOD";
}else{
mysql_select_db( 'c3chickens' );
mysql_query( "INSERT INTO chickens ('nickname', 'score') VALUES ( null, null, '".
mysql_real_escape_string( $_REQUEST['login'] ).
"', '".
mysql_real_escape_string( $_REQUEST['score'] ).
"')" );
$result = $mysqli->query($query);
}
?>
I didn't use PHP before, so I don`t know this language. I read examples in internet and tried a lot of variants of this php file, but no results. I suspect that I missed some important and simple detail.