【发布时间】:2011-11-07 05:45:52
【问题描述】:
我正在尝试使用 GET 从我的 iPhone 应用程序向在线表单发送数据(用户名和密码)。
这是我的源代码,但问题是没有数据存储在我的数据库中。
NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",enterUserName.text,enterPass.text];
NSURL *url=[NSURL URLWithString:@"http://planetimpressions.com/contacts/imagecomm_register.php"];
NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects
*/
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);
网页表单的源代码如下。
if(isset($_GET['submit'])){
$email = $_GET['email'];
$password = $_GET['password'];
$conn = mysql_connect("mysql", "*****", "*****");
if(!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("imagecomm_users");
$sql = "INSERT INTO registration(number, email, password) VALUES (null,
'$email', '$password')";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
}
?>
我已经测试了这些脚本,它们工作得非常好。我可以使用在线表格将用户名和密码存储到数据库中。我遇到的唯一问题是从应用程序连接到在线表单。
我没有在控制台中收到任何错误消息或任何意外消息。只是,什么都没有发生。我将不胜感激任何帮助。谢谢
【问题讨论】:
-
有错误吗?最后的日志是否显示出任何意外情况?网络上的数据包跟踪是否显示了您的预期?您是否调试过服务器以查看它正在接收什么?你知道数据库连接正确吗?这段代码和你的数据库之间有很大的差距,这段代码 sn-p 只解决了其中的一小部分。请在发布此类问题之前进行更多研究,或者如果您不知道如何进行该研究。
-
编辑了问题以反映您建议的更改
标签: php iphone ios get nsurlconnection