1
我是新的objective-c,現在我正試圖在iPhone上編寫一個簡單的用戶登錄程序。我的想法是使用ASIHTTPrequest中的類將信息發佈到與db鏈接的localhost中的php文件。我設置了一個按鈕,如果按下它,它會發出發布請求。但是,沒有插入到數據庫中。 這裏是我的代碼:ASIHTTPRequest簡單發佈失敗
.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;
}
?>
什麼「沒有按」工作「?錯誤訊息? – Mundi
首先嚐試使用** http:// **,則不支持ASIHTTP,請嘗試AFNetworking。 –