I’m new in objective-c, and I’m now trying to write a simple user-sign-in program on iPhone. My idea is to use classes in ASIHTTPrequest to post information to a php file in localhost which is linked to a db. I set up a button, and if you press it, it send out the post request. However, nothing is inserted to db.
Here’s my code:
.h file:
@interface ViewController : UIViewController
-(IBAction)post;
.m file:
#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 file:
<?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 file:
<?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;
}
?>
The problem is that you did not say to your request to start the request. Add
or
Also, since you have to become the delegate of request be sure to add methods (become delegate by:
[request setDelegate:self];):