doupo2157 2013-12-17 13:20
浏览 40
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • dongmei425373 2013-12-17 13:26
    关注

    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()); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码