双击时ios用户注册失败

时间:2014-07-06 07:54:08

标签: php ios ios7

我正在使用php脚本尝试在ios中进行成员注册以与localhost mysql进行通信。插入和检索效果很好。我现在正在尝试保护用户能够注册相同的电子邮件。这就是我所做的:

<?php 
header('Content-type: text/plain; charset=utf-8');


$db_conn = new  PDO('mysql:host=localhost;dbname=pal','root','root');
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$message = "";
$email = ($_POST['email']); 

$qry = $db_conn->prepare('SELECT email FROM users WHERE email = :email');
$qry->bindParam(':email', $email);
$qry->execute();
$result = $qry->fetchColumn();

//return $result;

echo utf8_encode($result);
?>

在我的视图控制器中,我这样做了:

-(IBAction)registrationBtn:(id)sender{
    NSString *email = self.emailField.text;
    NSString *password = self.passwordField.text;
    NSString *firstName = self.firstNameField.text;
    NSString *lastName = self.lastNameField.text;
    NSString *birthYear = self.yearField.text;
    /*String will contain Male/Femald */
    NSString *gender = [self.genderField titleForSegmentAtIndex:self.genderField.selectedSegmentIndex];

    if(email.length > 0 && password.length > 0 && firstName.length > 0 && lastName.length > 0 && birthYear.length > 0 && gender.length > 0)
    {

        BOOL userExist = [self checkIfUserExists:email];
        NSLog(@"Boolean Results:%hhd", userExist);
        if(userExist == false)
        {
            NSLog(@"User Doesn't Exist..Proceeding with Registration");
            /*All fields checked not empty, create raw string for HTTP*/
            NSString *rawStr = [NSString stringWithFormat:@"email=%@&password=%@&&firstname=%@&lastname=%@&birthyear=%@&gender=%@", email,
            password,
            firstName,
            lastName,
            birthYear,gender];

            NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:@"http://localhost:8888/registration.php"];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:data];


            NSURLResponse *response;
            NSError *err;
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

            NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
            NSLog(@"Response String: %@", responseString);

            NSString *success = @"success";
            [success dataUsingEncoding:NSUTF8StringEncoding];

            [self dismissViewControllerAnimated:YES completion:nil]; // Dismiss the viewController upon success
        }else{
            /*Issue error that user exists with this email*/
            NSLog(@"%s", "Error");
        }

    }

}

并且调用checkIfUserExist的函数也在这里:

-(BOOL)checkIfUserExists:(NSString*)userEmail
{
    NSString *rawStr = [NSString stringWithFormat:@"email=%@", userEmail];
    NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/checkUserExist.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];


    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];

    NSString *success = @"success";
    [success dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"Check User Reponse String: %@", responseString);
    if([responseString isEqualToString:userEmail])
    {
        NSLog(@"Check User = True");
        return true;
    }else{
        NSLog(@"Check User = False");
        return false;
    }
}

我用数据库表空启动模拟器。我在注册表中填写信息并单击按钮。数据存储在数据库中。下次我再次单击按钮寄存器时,它表示它存在,因此无法创建记录(正确的行为)。之后第三次左右点击次数,它不断添加并说用户不存在。

我个人认为问题是checkIfUserExist函数中的php响应并不总是从php脚本返回一串电子邮件。它有时返回null,有时是空的,有时是电子邮件。当它通过电子邮件返回时,操作按计划进行。

请帮忙。

根据要求,数据库结构:

enter image description here

0 个答案:

没有答案