Reputation: 147
I am having problems getting my post from my iOS app. I think it is the PHP file. I know it is connecting to the database ok, but it is not inserting the values into the SQL database.
Here is my PHP file in full:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$name =$_GET["name"];
$message = $_GET["message"];
mysqli_query INSERT INTO "test" ("", "name", "message",)
mysql_query($query) or die(mysql_error());
mysql_close();
?>
My iOS app is fine, as I am aware and sending the info out fine. But this is the first time i have used PHP.
I'm showing an error on line
mysqli_query INSERT INTO "test" ("", "name", "message",)
but not sure, how to solve.
My IOS Code IS as Follows
-(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 appendString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
-(IBAction)post:(id)sender{
[self postMessage:messageText.text withName:nameText.text];
[messageText resignFirstResponder];
messageText.text = nil;
nameText.text = nil;
}
new php code
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$name =$_GET["name"];
$message = $_GET["message"];
mysql_query("INSERT INTO test ('', $name, $message)") or die(mysql_error());
mysql_close();
?>
extra info
the app is just a view controller with a TextField , TextView, and a button as in the .h file
#define kPostURL @"http://www.dmk-media.com/taxi/tuttest.php"
#define kName @"name"
#define kMessage @"message"
@interface DMKFirstViewController : UIViewController{
IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;
}
-(IBAction)post:(id)sender;
and then to post the message
-(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 appendString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
and then the button action
-(IBAction)post:(id)sender{
[self postMessage:messageText.text withName:nameText.text];
[messageText resignFirstResponder];
messageText.text = nil;
nameText.text = nil;
}
thats all the is in the code its just a basic app to try and test posting to my sql server
Upvotes: 2
Views: 247
Reputation: 736
Remove:
$name =$_GET["name"];
$message = $_GET["message"];
...
mysqli_query INSERT INTO "test" ("", "name", "message",)
mysql_query($query) or die(mysql_error());
It should be:
$name = $_POST["name"];
$message = $_POST["message"];
mysql_select_db('DATABASE_NAME', $conn) or die(mysql_error());
...
mysql_query("INSERT INTO test(name, mesage) VALUES ('$name', '$message');", $conn) or die(mysql_error());
Upvotes: 5
Reputation: 1
-(void) postMessage: (NSString*) message withName: (NSString *) name{
if (name != nil && message != nil){
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
NSString *param = [NSString stringWithFormat:@"%@=%@&%@=%@",kName, name, kMessage, message];
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = @"POST";
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
Upvotes: 0