使用GET方法将整数发送到服务器

时间:2013-09-19 03:28:02

标签: mysql ios xcode nsurlconnection getmethod

我一直在研究这个问题已经有一段时间了,直到最近才发现GET方法足以满足我的需要。 我的目标:    从我的应用程序中取一个整数并将其发送到php服务器以保存它

现在,我只需要发送一个整数来使其工作并有一个起点可以工作。一旦我开始滚动并开始工作,我将一次最多发送20个整数,所以我不相信我对GET方法的数据量限制有问题。

我正在和这个项目的朋友一起工作,因为我不熟悉php,但我已经多次接触过它,所以我应该知道大部分涉及的术语。无论如何,这是我朋友为我写的PHP代码......

<?php
// A sample php file to demo passing parameters and getting POST data.
// This simply appends the specified value to the end of the 'sample' table.
// Call it like this: sample.php?val=4
//    where 4 is the value you want to append to the table
// The code returns whether or not the sql query was performed with success or not.
//    If successful, a boolean true (or 1) is returned.
//    If not, then an error message is returned with the sql error.

   include 'dbConnect.php';

   $val = $_GET["val"];  // value to set to

   $sql=
   "INSERT INTO sample (`test`)
   VALUES ('" . $val . "')
   ";

   $result = mysql_query($sql,$con);
   if(!$result){
      die('Error: ' . mysql_error());
    }

   echo $val+1;

   mysql_close($con);

?>

此代码获取变量“val”的发送值并回显该值加1(以便设置和获取值与站点不同,以便我可以更轻松地判断我的代码是否正常工作)这应该正常工作?

接下来,我相信我还没有完成项目的Xcode部分,但我有一个成功的连接发生,我只是无法在php服务器中更改变量的值。在我的项目中,我有一个名为“num”的NSInteger变量,它的值由我的应用程序中的文本字段设置,是我要发送到服务器的值。我也使用一个按钮(称为“postPressed”)来启动发送过程的功能。所以这是我的代码...

NSURLRequest *request;
NSURLConnection *connection;
NSURL *url;

NSInteger num;



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (IBAction)valueChange:(id)sender {
    num = [_valueTF.text intValue];
}


- (IBAction)postPressed:(id)sender {
    url = [NSURL URLWithString: [NSString stringWithFormat: @"http://jrl.teamdriven.us/source/scripts/2013/sample.php?val=%d", num]];
    request = [NSURLRequest requestWithURL:url];
    if (request) {
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        _returnLbl.text = @"success!";
    }

}

我的returnLbl确实变为“成功”,所以我知道连接有效,我只是觉得我在设置变量部分时缺少一些代码。请帮助我,这已经在我身边荆棘大约一个月了。此外,我为这个问题的长度道歉,我只是想了解所有细节,以便不必做任何澄清。

1 个答案:

答案 0 :(得分:0)

所以,事实证明我的代码一直都在工作,只是当我查找phpMyAdmin生成的数据表时,我没有注意到有一个第二页数据,因为我测试了它在我的浏览器中。使用上面的代码成功发送和保存整数。我为发布这样的事情而道歉,并找出真正的错误是多么愚蠢,但如果有人愿意给我任何改进,我愿意接受他们。感谢您抽出宝贵时间查看此问题。

相关问题