我研究这个问题已经有一段时间了,并且一直在尝试使用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“的发送值,并回显该值加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确实变成了“成功”,所以我知道连接是有效的,我只是觉得我遗漏了一些设置变量部分的代码。请帮帮我,这已经让我头疼了大约一个月了。另外,我为这个问题的冗长道歉,我只是想了解所有的细节,这样就不需要任何澄清了。
发布于 2013-09-22 14:06:41
所以,事实证明我的代码一直都在工作,只是当我查找phpMyAdmin生成的数据表时,我没有注意到有一个第二页的数据,因为我在浏览器中测试了太多。使用上面的代码成功发送并保存了整数。我很抱歉发布了这样的东西,并发现这真的是一个多么愚蠢的错误,但如果有人愿意给我任何类型的改进,我愿意接受他们。感谢您花时间查看此问题。
https://stackoverflow.com/questions/18885722
复制相似问题