drzrdc1766788 2015-08-24 23:41
浏览 25

iOS和PHP:超时错误

It has been about 4 years since I last wrote PHP code. I am trying to write a web service that would insert into a MySQL database table. I'm passing the data via POST. In my app I am using NSURLConnection and it is initing ok with the NSURLRequest. But then I am getting a timeout error. When I check the db table it's cardinality is still 0 so I know the data is not being inserted. And I am not getting any hits on the NSURLConnection delegate methods. My suspicion is that there is something wrong with my PHP code. Could someone take a look at this and let me know if they see something odd.

I know the URL is correct. I also know the POST data is being attached to the request ok.

Here's the iOS code:

- (BOOL) submitAccountData : (NSArray*) arrAccountTextFields {

    BOOL success = YES;

    //set up url
    NSURL* accountURL = [NSURL URLWithString:kAccountService];
    NSMutableURLRequest* accountURLRequest = [NSMutableURLRequest requestWithURL:accountURL];

    NSMutableDictionary* dictPostData = [[NSMutableDictionary alloc] init];
    for(KIP_TextField* thisTextField in arrAccountTextFields) {
        NSString* strKey = thisTextField.placeholder;
        strKey = [strKey stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        strKey = [strKey stringByReplacingOccurrencesOfString:@" " withString:@""];

        if (thisTextField.myTextFieldType == SECURE_TEXTFIELD) {
            strKey = [strKey stringByReplacingOccurrencesOfString:@"word" withString:@""];
        }
        [dictPostData setValue:thisTextField.text forKey:strKey];
    }

    //add post data
    accountURLRequest = [self postData:accountURLRequest :dictPostData];

    //uncomment below to see what post data is being sent
    //NSString* strPOSTData =  [NSString stringWithUTF8String:(char*)[[accountURLRequest HTTPBody] bytes]];
    //NSLog(@"%@", strPOSTData);

    NSURLConnection* myURLConnection = [[NSURLConnection alloc] initWithRequest:accountURLRequest delegate:self];

    if(myURLConnection) {

        [myURLConnection start];

    } else {
        NSLog(@"Connection could not be made");
        BOOL success = NO;
    }

    return success;
}

Here's the PHP code.

<?php


    //get passed variables
    $userName = $_POST['UserName'];
    $userCity = $_POST['UserCity'];
    $userState = $_POST['UserState'];
    $userStateAbbrev = $_POST['UserStateAbbrev'];
    $userEmail = $_POST['UserEmail'];
    $userPass = $_POST['UserPass'];

    //put into an array
    $arrUserInfo = arrasy($userName, $userCity, $userState, $userStateAbbrev, $userEmail, $userPass);

    $servername = "ramjamlaxcom1.ipagemysql.com";
    $username = "mpl_admin";
    $password = "mpl_admin";
    $dbname = "mpl_main";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        echo "Connection failed!";
        die("Connection failed: " . $conn->connect_error);
    }

    mysql_select_db(mpl_main); 

    //create prepared statement
    $stmt = $dbh->prepare("INSERT INTO accounts (userName, userCity, userState, userStateAbbrev, userEmail, userPass) VALUES (:userName, :userCity, :userState, :userStateAbbrev, :userEmail, :userPass)");

    $stmt->bindParam(':userName', $userName);
    $stmt->bindParam(':userCity', $userCity);
    $stmt->bindParam(':userState', $userState);
    $stmt->bindParam(':userStateAbbrev', $userStateAbbrev);
    $stmt->bindParam(':userEmail', $userEmail);
    $stmt->bindParam(':userPass', $userPass);

    $stmt->execute();

    //check for success
    if (mysql_affected_rows() > 0) { 

        echo "success";

    } else { 

        echo "failed";
    }


?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度