I'm learning how to use xcode with php and mysql online database.
I made an app that just takes a user input (name) via a TextField and, when pressing a button, it sends this name to the already created online database.
That was my idea... but I can't make it work! And I don't know what could possible be wrong, since I never done this before...
I would be very grateful if anyone can please help me solving this... I looked a lot of sites (even here) but I cannot make it work.
Thank you all!
Here is my code:
- (IBAction)send:(id)sender {
NSString *post = [NSString stringWithFormat:@"name=%@", nome.text];
NSString *removeSpaces = [NSString stringWithFormat:@"%@", [post stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
const char *urlUTF8 = [removeSpaces UTF8String];
NSString *postBody = [NSString stringWithUTF8String:urlUTF8];
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postDataLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.ultrasoft.com/iossignup.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postDataLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
}
and my PHP file:
$DB_HostName = "DOMAIN";
$DB_Name = "DB";
$DB_User = "USER";
$DB_Pass = "PASS";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($DB_Name,$con) or die(mysql_error());
if (isset ($_POST["name"])) {
$name = $_POST["name"];
}
$sql = "INSERT INTO user ('nome') VALUES ('$name') ";
$res = mysql_query($sql,$con) or die(mysql_error());