我一直在研究这个问题一段时间,并且一直在尝试使用 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 确实更改为“成功”,所以我知道连接有效,我只是认为我在设置变量部分时缺少一些代码。请帮帮我,这已经让我头疼了大约一个月了。另外,我为这个问题的长度道歉,我只是想了解所有细节,这样就不必做任何澄清了。