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 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名