私はobjective-cが初めてで、iPhoneで簡単なユーザーサインインプログラムを作成しようとしています。私の考えは、ASIHTTPrequest でクラスを使用して、db にリンクされている localhost の php ファイルに情報を投稿することです。ボタンを設置して、押すと投稿リクエストを送ります。ただし、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;
}
?>