【问题标题】:Send an integer into a server with GET method使用 GET 方法将整数发送到服务器
【发布时间】:2013-09-24 00:13:10
【问题描述】:

我一直在研究这个问题一段时间,并且一直在尝试使用 POST 方法,直到最近我发现 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”的发送值并回显该值加一(以便设置和获取值与站点不同,因此我可以更轻松地判断我的代码是否有效)这应该可以正常工作?

接下来,我相信我还没有完成项目的 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. 不要使用mysql_ 函数! 和 2. 你易受 SQL 注入攻击

标签: mysql ios xcode nsurlconnection getmethod


【解决方案1】:

所以,原来我的代码一直在工作,只是当我查看phpMyAdmin生成的数据表时,我没有注意到有第二页数据,因为我已经测试过了在我的浏览器中。整数已使用上面的代码成功发送和保存。对于发布这样的内容并发现它确实是多么愚蠢的错误,我深表歉意,但如果有人想给我任何改进,我对他们持开放态度。感谢您抽出宝贵时间查看此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2015-11-18
    • 2016-10-10
    • 1970-01-01
    • 2022-01-17
    • 2017-10-19
    相关资源
    最近更新 更多