I am currently trying to send data from my xcode project to a server that I had made but the data doesn't seem to appear in my tables when I run my application.
It is quite simple, allowing the user to enter a name and a message which is sent when a button is pressed. I do not know what I've done wrong, any suggestions?
Here is my php code:
<?php
$username = "root";
$database = "testdb";
mysql_connect('localhost', $username);
@mysql_select_db($database) or die ("Unable to find database");
$name = @$_GET["name"];
$message = @$_GET["message"];
$query = "INSERT INTO test VALUES ('', '$name', '$message')";
mysql_query($query) or die (mysql_error("error"));
mysql_close();
?>
and my obj c code:
.h
#import <UIKit/UIKit.h>
#define kPostURL @"http://localhost/TESTCONNECT.php" //variable to use whenever
#define kName @"name"
#define kMessage @"message"
@interface FirstViewController : UIViewController{
IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;
}
-(IBAction)post:(id)sender;
@end
.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) postMessage:(NSString*) message withName:(NSString *) name{
if(name !=nil && message !=nil){
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kName, name]];
[postString appendString: [NSString stringWithFormat:@"&%@=%@", kMessage, message]];
[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];
}
}
-(IBAction)post:(id)sender{
[self postMessage:messageText.text withName:nameText.text];
[messageText resignFirstResponder];
messageText.text=nil;
nameText.text=nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end