我是objective-c 的新手,我现在正在尝试在iPhone 上编写一个简单的用户登录程序。我的想法是使用 ASIHTTPrequest 中的类将信息发布到 localhost 中的 php 文件,该文件链接到 db。我设置了一个按钮,如果你按下它,它就会发出 post 请求。但是,没有向 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>';
}
?>
数据库.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;
}
?>