vendredi 11 septembre 2015

Insert data to MySQL database - iOS

I'm trying to insert data to table in MySQL database but it's not working. Code in .php file is correct but I think is something wrong with my app in objective-c. Problem is the data is not insert to database. Could you help me how can I insert data?

This is .php file:

<?php

    if (isset ($_POST["name"]) && isset ($_POST["age"])){
        $name = $_POST["name"];
        $age = $_POST["age"];
    }
    else {
        $name = "";
        $age = "";
    }

try {

    $con = new PDO("my domain without http://", "login", "password");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO NameOfDatabase.NameOfTable (NameOfColumn1, NameOfColumn2) VALUES ('$name', '$age')";
    // use exec() because no results are returned
    $con->exec($sql);
    echo "New record created successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$con = null;

?>

and this is code in app (objective-c):

NSString *strURL = [NSString stringWithFormat:@"my domain without http://?name=%@&age=%@", self.name.text, self.age.text];
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    NSLog(@"%@", strResult);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire