我是objective-c的新手,我现在正试图在iPhone上编写一个简单的用户登录程序。我的想法是使用ASIHTTPrequest中的类将信息发布到localhost中的一个php文件,该文件链接到db。我设置了一个按钮,如果你按下它,它会发出一个帖子请求。但是,没有任何内容插入到db。 这是我的代码:
.h文件:
@interface ViewController : UIViewController
-(IBAction)post;
.m文件:
#import "ViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
@implementation ViewController
-(IBAction)post {
NSURL *url = [NSURL URLWithString:@"localhost:8888/index.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"paul" forKey:@"user"];
[request setPostValue:@"12345" forKey:@"pw"];
}
index.php文件:
<?php
include('database.php');
session_start();
$link = connect_mysql();
mysql_select_db("test",$link);
if (($_POST["user"]=="")||($_POST["pw"]=="")) { header('Location:error.html'); exit(); }
$sql = 'select * from userinfor where name="'.$_POST["user"].'";';
$result = mysql_query($sql, $link);
$pass= mysql_fetch_object($result);
if ($pass) { header('Location:again.html'); exit();}
if ($_POST["pw"]!=$_POST["pw2"]) { header('Location:error2.html'); exit();}
$sql = 'INSERT INTO userinfor (name, password) values ("'.$_POST["user"].'","'.$_POST["pw"].'")';
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: '. mysql_error();
exit();
} else {
echo 'Sucessfully added entry<br/>';
echo '<a href="login.html">Click to login</a>';
}
?>
database.php文件:
<?php
function connect_mysql() {
//Connect to database (Please specify the password field if you needed
$link = mysql_connect('localhost', 'root', '12345');
if (!$link) {
die('Could not connect to mysql');
}
$db = mysql_select_db('test', $link);
if (!$db) {
die('Could not select database');
}
return $link;
}
?>
答案 0 :(得分:0)
问题是您没有说出要求启动请求。添加
[request startAsynchronous];
或
[request startSynchronous];
此外,由于您必须成为请求的委托,请确保添加方法(成为委托人:[request setDelegate:self];
):
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;