1

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.

D.Cher
  • 13
  • 2

1 Answers1

0

Never mix mysql with mysqli.

Write your php code as below.

// Connection object
$con = mysqli_connect($hostname, $username, $password, $dbnamemysql) or die("Some error occurred during connection " . mysqli_error($con));
// Get data in variables. Here use mysqli_real_escape_string
$login =  mysqli_real_escape_string($con, $_REQUEST['login']);
$score =  mysqli_real_escape_string($con, $_REQUEST['score']);
// Query execution
$result = mysqli_query($con , "INSERT INTO chickens('nickname', 'score') VALUES('$login', '$score')" );
// $result will return true or false
if($result){
    echo 'success';
}else{
   echo("Error description: " . mysqli_error($con));
}

Write below two lines at starting of your file:-

// It will show all php warning, notice, and errors.
error_reporting(E_ALL);
ini_set('display_errors', 1);

Hope it will help you :)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • error is "Notice: Undefined index: login in /var/www/clients/client3/web21/web/php.php on line 26" . So it doesnt see login, added from xcode. – D.Cher Feb 02 '16 at 17:34
  • Have you passed 'login' and 'score' both keys ? Pass then in POST method or GET method. – Ravi Hirani Feb 02 '16 at 17:37