从iOS代码接收数据的PHP文件不起作用

时间:2013-12-17 13:20:16

标签: php ios sql insert

我在iOS应用中收到帖子时遇到问题。我认为这是PHP文件。我知道它连接到数据库ok,但它没有将值插入SQL数据库。

这是我的PHP文件:

<?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();

?>

我的iOS应用程序很好,因为我知道并且发送信息正常。但这是我第一次使用PHP。

我在线上显示错误

       mysqli_query INSERT INTO "test" ("", "name", "message",)

但不确定,如何解决。

我的IOS代码如下

-(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;



}

新的PHP代码

$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();

?>

额外信息


该应用只是一个视图控制器 TextField,TextView和.h文件中的按钮

#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;

然后发布消息

-(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;



}

这就是代码中的所有内容,它只是一个尝试测试发布到我的sql server的基本应用程序

2 个答案:

答案 0 :(得分:5)

卸下:

$name =$_GET["name"];
$message = $_GET["message"];

...

mysqli_query INSERT INTO "test" ("", "name", "message",)
mysql_query($query) or die(mysql_error());

应该是:

$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()); 

答案 1 :(得分: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];
    }
}