2

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

4
  • First: you are mixing mysql and mysqli. Second: does your iOS app call a GET or POST request? Commented Dec 17, 2013 at 13:26
  • post check my edit i have put in the ios code Commented Dec 17, 2013 at 14:16
  • So you do get data from iOS app but can't store it to database? Leonardo's answer should help. The problem is you are mixing mysql and mysqli: this are two different APIs Commented Dec 17, 2013 at 14:19
  • i have changed that area but still not posting results Commented Dec 17, 2013 at 14:38

2 Answers 2

5

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()); 
Sign up to request clarification or add additional context in comments.

11 Comments

@MikeAsp check my updated answer, I changed this line: mysql_query("INSERT INTO test ('', $name, $message)", $conn) or die(mysql_error());
that got rid of the errors. but im still getting no data going in to the SQL database. the iphone seems to be posting ok but the data is getting lost somewhere
use a DBMS like MySQL Workbench or phpMyAdmin and execute this query: SHOW COLUMNS FROM test and post that response in your question. What I want to see is the data types and keys of your columns
@MikeAsp see my updated answer: I changed this line: mysql_query("INSERT INTO test(name, mesage) VALUES ($name, $message)", $conn) or die(mysql_error()); . This should definitely work...
@MikeAsp I updated the last line of my answer..it should work!
|
0
-(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];
    }
}

2 Comments

you should add a quick description of what was changed and what your doing for more clarity
added extra details at the bottom of my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.